0

I would like to apologize if this has a trivial answer but I can't seem to come up with a pattern/flow for this scenario.

I am using Android Asynchronous Http Client which provides a callback for when its done with the request. I guess this is where I'm getting confused because I don't know where to use the library.

Here is my setup:

DataClass.java

 public class DataClass {
     String created_date = null;
     String username = null;
     String details = null;
     private static AsyncHttpClient client;

     public DataClass(String id) {
         client = new AsyncHttpClient();
         GetDataObject(id);
     }

     public void GetDataObject(String id) {
          //...build RequestParams()
          client.post(get_data_url, params, new JsonHttpResponseHandler(){
              public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                 String response_string = new String(response);
                 //...parse JsonObject
                 created_data = jsonObject.getString("date");
                 username = jsonObject.getString("username");
                 details = jsonObject.getString("details");
                 // all dataClass instance variables accessed here are null.
                 Log.d("Created: ", created_data); //returns somedate
                 Log.d("Username: ", username); //returns someusername   
              }
          });
     }
 }

ShowDataActivity.java

 public class ShowDataActivity extends Activity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_show_question);

         Intent intent = getIntent();
         String id = intent.getStringExtra("id");
         DataClass dataClass = new DataClass(id);

         // all dataClass instance variables accessed here are null.
         Log.d("Created: ", dataClass.created_data); //returns null
         Log.d("Username: ", dataClass.username); //returns null

     }
 }

When I put breakpoints on the instance variables assignment inside the RestClient callback, I can see that they aren't null but have the correct data in them.

I have been searching for a while and see some built-in Android libraries like Loaders, ContentObserver and BoradcastReceivers that might help but I don't know how to use them in conjunction with Android Asynchronous Http Client.

Please not the example above is not complete, but all the relevant information is there.

Let me know if more information is needed. Thank you.

Problem: When I instantiate the DataClass in ShowDataActivity my dataClass instance variables are null. I'm guessing it's because the variables are being assigned in the callback which is after I instantiate the object. Not really sure how to ask this but how can I make sure that DataClass dataClass = new DataClass(id); initializes with the data from the internet.

morten.c
  • 3,414
  • 5
  • 40
  • 45
Archon
  • 182
  • 9
  • What is your question? – Code-Apprentice Jan 13 '14 at 22:02
  • You've described what you're doing, but what's the problem with it? (Particularly since your variables are being correctly set, the problem is unclear.) Also, what is `RestClient`? – Ted Hopp Jan 13 '14 at 22:02
  • Just updated the question, please note the `RestClient` changes too. – Archon Jan 13 '14 at 22:22
  • You haven't told us what you are trying to do with the data received and since it's correct, you appear to have no issues and no question! – Simon Jan 13 '14 at 22:22
  • Following your edit, you can't since the object is created before you receive your data. To continue the chain, **why** do you need these variables to be non-null when you create the instance of the class? – Simon Jan 13 '14 at 22:24
  • Because I would like to use the data to populate Textview's and whatnot. Not really sure what you meant by that. Maybe I didn't understand you. – Archon Jan 13 '14 at 22:30
  • 1
    But surely the data in the vars is only valid once you've fetched it? Normal practice is to show a wait cursor during loading, disable controls until loaded, not showing the activity until the data is available etc. I'm still not clear on what you are trying to achieve. – Simon Jan 13 '14 at 22:49

0 Answers0