0
String name;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getData();
    this.setText(name);
}
private void getData(){
    name = object.getString("name");
}

I'm just making my line of code short, anyway the idea is already there. So on the method getData() I'm retrieving data from the web. I am able to test and was able to get the result however when I put the name on the onCreate() it gives me a null value. How to do some work arounds on this?

public class Testing extends NavigationLiveo{
String name;

@Override
public void onInt(Bundle savedInstanceState) {
    getData();
    Log.wtf("name0", name);
    this.username.setText(name);
}
private void getData() {
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    StringRequest sr = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            try {
                name = object.getString("name");
                Log.wtf("name1", name);
            } catch (Exception e) {
                Log.wtf("testing1", e.getMessage());
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("user", user_pid);
            return params;
        }
    };queue.add(sr);
}
}

Once again, I've made my coding simple. I can really get the data from the web server. It's just that whenever the I'll try to access the name on the onInt(Bundle savedInstanceState) it returns a null value. The log says it all as well. "name1" returns a value while "name0" returns null.

fmpsagara
  • 485
  • 5
  • 17

2 Answers2

0

Can you put the getData() code?
I think that since the method uses AsyncTask or so to download the data from URL asynchronously. That means the method hasn't completed downloading, the setText(name) code already executed. So it displays null.

Rao
  • 20,781
  • 11
  • 57
  • 77
Pham Van Vung
  • 223
  • 3
  • 6
  • I'm 100% sure that it's getting the result from the web. By the way I'm using volley instead of AsyncTask. It's just that the value retrieved from the method stays within the method. I'll be posting the code – fmpsagara Feb 27 '16 at 10:07
  • Hi Volley is also asynchronous, so you should write this `this.setText(name);` in the `onResponse(String response)` method of your request (e.g., `StringRequest`) to get it executed once the request is done. – Pham Van Vung Feb 27 '16 at 10:21
  • Yeah that was what I was thinking however the `this.username.setText()` can only be used within the onInt(Bundle bundle). I'm using a library by the way. – fmpsagara Feb 27 '16 at 10:33
  • So, I think you could test the RequestFuture class for a synchronous request instead of asynchronous request. – Pham Van Vung Feb 27 '16 at 14:35
  • I'm sorry I'm not familiar with that. I tried passing data to a singleton class and then tried to retrieve it but still giving me a null value :( – fmpsagara Feb 27 '16 at 14:58
  • You can find an example from this post http://stackoverflow.com/questions/16904741/can-i-do-a-synchronous-request-with-volley – Pham Van Vung Feb 27 '16 at 15:08
  • Thanks for the suggestion. It's kinda annoying that I'm stuck with this issue. It's so frustrating. – fmpsagara Feb 27 '16 at 15:54
0

The only explanation is that the object.getString("name") returns null, or you are setting it to null somewhere afterwards. By the way, when retreiving data from the web, you should use AsyncTask, for more details refer here.

Rao
  • 20,781
  • 11
  • 57
  • 77
  • I'm 100% sure that it's getting the result from the web. By the way I'm using volley instead of AsyncTask. It's just that the value retrieved from the method stays within the method. I'll be posting the code – fmpsagara Feb 27 '16 at 10:18