0

i was wondering what is the best practice to get a JSON and to display it, from one to activity(with NO GUI) to an other activity(WITH GUI),i found couple methods ranging from just passing the jsonobject in the PutExtra, to making a Parcable class and then passing it as an PutExtraParacble. but i found it incomplete because getting a json can vary from 1milisec to 5 whole second's(going to have samll and huge Jsons) so when i use the parcable class method and then get the extras from my MainActivity i can get a null pointer exception from the parcable class... here is the importent code parts:

     APIcontroller.getCardAPI("http://www.somesite.com/feeds/GetPlace", params);
    Intent getJson = getIntent();
    CardContent cardContent = (CardContent) getJson.getParcelableExtra("cardContent");
    jsonArryCard = cardContent.getJsonArray();

and then in the APIcontroller class:

 public void getCardAPI(String WSaddress,RequestParams params) {
    AsyncHttpClient client = new AsyncHttpClient();
    client.post(WSaddress,params, new AsyncHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            try {
                String respons = null;
                try {
                    respons = responseBody == null ? null : new String(responseBody, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                JSONObject responseObject = new JSONObject(respons);
                JSONArray resultsArray = responseObject.getJSONArray("places");
                JSONObject taskObject = null;
                for (int i = 0; i < resultsArray.length(); i++) {
                    // the JSON object in position i
                    taskObject = resultsArray.getJSONObject(i);

                    String temp = (String) taskObject.get("UserName");
                    Log.e("String test", temp);

                }
                CardContent cardContent = new CardContent(resultsArray);
                Intent sendjson = new Intent(APIController.this, MainRegisterdActivity.class); 
                sendjson.putExtra("cardContent", (Parcelable) cardContent);

                startActivity(sendjson);
                MainRegisterdActivity.class);
                Log.e("onSuccess", String.valueOf(taskObject));

                } catch (JSONException e) {
                e.printStackTrace();
            }
        }

i think its because the asyntask is still runing so it dosent have a value when its goes to the next code line.. should i just use broadcast reciver or is there any other way? thanks for any help

rollcage
  • 101
  • 1
  • 10

1 Answers1

0

You are thinking right! I had the same experience.In my opinion, the best way is to use broad cast receiver. Another untidy solution is to take getCardAPI function out of your class and place it in activity with GUI and place rest of your code in getCardAPI function.Thus,you are sure that value is set and rest of your code will be executed, but it is not recommended!

Koorosh
  • 457
  • 5
  • 14