Ubidots Community

[SOLVED] I am not getting output for the code though its correct

public class Login_check extends AppCompatActivity {
extView tw, tw2, tw1;

            String engine_status;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.login_page);
                tw = (TextView)findViewById(R.id.text_print);
                tw1 = (TextView)findViewById(R.id.textveiw1);
                tw2 = (TextView)findViewById(R.id.textView2);
                tw1.setText("here");
                AsyncTask<Void, Void, Value> val =  new ApiUbidots().execute();
                tw2.setText("here after val initialized");
                tw.setText(val.toString());
            }
        }
        
        class ApiUbidots extends AsyncTask<Void, Void, Value>{
            private final String API_KEY = "xxxx";
            private  final String VARIABLE_ID = "xxxx";
        
            @Override
            protected Value doInBackground(Void... params) {
                ApiClient api = new ApiClient(API_KEY);
                Variable engine_change = api.getVariable(VARIABLE_ID);
                Value val = engine_change.getValues()[0];
                return val;
            }
        
         
            @Override
            protected void onPostExecute(Value value) {
                super.onPostExecute(value);
            }
        }

its urgent @junada95


the above is the output i got instead of the val from the variable i am getting “com.example.lenovo.junoracers.ApiUbidots@some random number with alphabets”

Can you please provide more context? Is this a Android app? What’s the current output or error message when trying to run the code?

i did it please suggest any corrections its urgent…i hav to submit my project…@hackmed

Hi @ayshwarya, you’re getting that String because you’re printing the information from the AsyncTask Class ApiUbidots you’ve created.

The line tw.setText(val.toString()); should be added instead in the onPostExecute method of your AsyncTask class. And the value you’re returning is the one you receive via the parameters of that method.

Your onPostExecute method may be kind of like this:

protected void onPostExecute(Value value) {
    String val = String.valueOf(value.getValue());
    tw.setText(val);
}

Regards

where can i get my id for a particular Data source @junada95

At this moment, the only way to get the ID for a particular Data Source in our website is to access to that Data Source and copy the string after the last /

The highlighted line should be your Data Source ID.

Regards

what does t

his error mean.! its shwing near the ProcessDialoge

what if i want to set the text view outside of the class extending AsyncTask?
why i cant see the result in Toast and not in the text view?

public class Login_check extends AppCompatActivity {
    public TextView tw;
    public static String engine_status;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_page);
        tw = (TextView) findViewById(R.id.text_print);
        AsyncTask<Void, Void, String> val = new ApiUbidots().execute();
        tw.setText(val.toString());
        Log.i("info", "onCreate: " + engine_status);
    }
    class ApiUbidots extends AsyncTask<Void, Void, String>{
        ProgressDialog progres;
        private final String API_KEY = "028dc5b1feab66c7f9884788424b4b621d195e04";
        private final String VARIABLE_ID = "57963d71762542081a480bcb";

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progres = ProgressDialog.show(Login_check.this, "loading....", "loading...", false);
        }

        @Override
        protected String doInBackground(Void... params) {
            ApiClient api = new ApiClient(API_KEY);
            Variable engine_change = api.getVariable(VARIABLE_ID);
            Value val = engine_change.getValues()[0];
            return String.valueOf(val.getValue());
        }

        @Override
        protected void onPostExecute(String val) {
            Toast.makeText(Login_check.this, val, Toast.LENGTH_SHORT).show();
            super.onPostExecute(val);
            progres.dismiss();
        }
    }
}

@juanda95

Because you’re receiving the actual Value from Ubidots in the postExecute method, after you processed it in the doInBackground method.
Then, the Toast shows you the data you actually want to view (the last value of your Variable).

In the TextView you’re not able to print the last value from Ubidots because you’re really printing the memory location of your ApiUbidots class.

If you want to show the Value you receive from Ubidots, you should call the setText() method of your TextView to the postExecute() method of your ApiUbidots class.

i want to refresh the data in my app i.e get data from udibots i am using runnable and its showing there is an run time error that is some Looper error.is there any other way…?

void display() {
        get_engine_status.execute();
        temperatue_status.execute();
        speed_status.execute();
        belt_status.execute();
    }

this is the actual function which gives me status of the variables
i want to repeat this function for evry 10 secs @junada95

Thanks @juanda95 I’m new to Android development as well