1

Response of my json is given below,and I am trying to parse data,but when I parse user_photo_id,it gives JSONException:No values for user_photo_id,can any one tell me?

{  
  "user_login_id": "2650",  
  "user_total_photo": "3",  
  "max_upload_photo": "3",  
  "image_list": [  
    {  
      "user_photo_id": 334,  
      "status": "Approved",  
      "photo": ""  
    },  
    {  
      "user_photo_id": 394,  
      "status": "Approved",  
      "photo": ""  
    },  
    {  
      "user_photo_id": 398,  
      "status": "Admin Approve Remaining",  
      "photo": ""  
    }  
  ]  
}

my java code:

 class LoadImages extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {


         String photoid;
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
            String jsonStr = sh.makeServiceCall(IMAGE_URL, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);


                try {
                    jsonObj = new JSONObject(jsonStr);


                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(IMAGE_USERLOGIN_ID, jsonObj.getString(IMAGE_USERLOGIN_ID));
                        map.put(IMAGE_TOTAL_PHOTO,jsonObj.getString(IMAGE_TOTAL_PHOTO));
                        map.put(IMAGE_MAX_UPLOAD, jsonObj.getString(IMAGE_MAX_UPLOAD));
                        /*map.put(IMAGE_USER_PHOTOID, jsonObj.getString(IMAGE_USER_PHOTOID));
                        map.put(IMAGE_STATUS, jsonObj.getString(IMAGE_STATUS));
                        map.put(IMAGE_PHOTO, jsonObj.getString(IMAGE_PHOTO));
                        */




                        image_list = (JSONArray) jsonObj.get(IMAGE_LISTING);
                        for(int i=0;i< image_list.length();i++)
                        {
                           photoid += image_list.get(i);
                           Log.d("mylog", "initial marital  = " + photoid);

                        }

                          getActivity().runOnUiThread(new  Runnable() 
                     {
                        @Override
                        public void run() 
                        {
                            Intent intent=new Intent(getActivity(),PhotoView.class);
                            intent.putExtra("id", id);
                            intent.putExtra("totals", totalphota);
                            intent.putExtra("maxs", maximumphota);

                            startActivity(intent);
                        } 
                    });
                       /* image_list = (JSONArray) jsonObj.get("user_photo_id");
                        image_list=(JSONArray) jsonObj.get(IMAGE_STATUS);
                        image_list=(JSONArray)jsonObj.get(IMAGE_PHOTO);
                        for(int i=0;i< image_list.length();i++)
                        {
                          String photoid=

                        }*/

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


            return data;
        }


        protected void onPostExecute(ArrayList<HashMap<String,String>> result) {

            super.onPostExecute(result);


            // dismiss the dialog after getting all albums
            if (pDialog.isShowing())
                pDialog.dismiss();
            // updating UI from Background Thread
            /*Intent mIntent = new Intent(getActivity(),Filter.class);
            Bundle extras = mIntent.getExtras();
            try {
                extras.putString("radiostatus", jsonObj.getString(FILTER_ALLOW));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  */


        }

    }
6
  • Just write jsonObj.getInt(IMAGE_USERLOGIN_ID); Commented Dec 25, 2014 at 6:21
  • i dont want user login id,i want userphotoid Commented Dec 25, 2014 at 6:22
  • just write this jsonObj.getInt("user_photo_id"); Commented Dec 25, 2014 at 6:24
  • mage_list = (JSONArray) jsonObj.getInt(IMAGE_USER_PHOTOID); i tried like this..and it shows Cannot cast from int to JSONArray Commented Dec 25, 2014 at 6:25
  • can you show me parse code ? Commented Dec 25, 2014 at 6:25

2 Answers 2

1

Hope that helps. You just need to follow the structure of your json string. ( The json array contains json objects so get the json object first in the loop, then from that get the user_photo_id )

image_list = (JSONArray) jsonObj.get("image_list");
for(int i=0;i< image_list.length();i++)
{
   JSONObject imageListItem = image_list.getJSONObject(i);
   int  userPhotoId = imageListItem.getInt("user_photo_id")
   Log.d("mylog", "i ="+i+" and user_photo_id =" + userPhotoId);
}
Sign up to request clarification or add additional context in comments.

Comments

1
image_list = (JSONArray) jsonObj.get(IMAGE_LISTING);
for(int i=0;i< image_list.length();i++)
{
    photoid += image_list.get(i);//here you made a mistake
    Log.d("mylog", "initial marital  = " + photoid);

}

In your json , image_list is JsonArray , But each element in this array is an JsonObject, so you will get a JsonObject use image_list.get(i) not int. So you should do more task:

image_list = jsonObj.getJSONArray(IMAGE_LISTING);
for(int i=0;i< image_list.length();i++)
{
    photoid += image_list.getJSONObject(i).getInt("user_photo_id");
    Log.d("mylog", "initial marital  = " + photoid);

}

Update to send json data to other Activities:

1.Create a class to store your data like this :

public class JsonData{
    private JsonObject mData;

    //getter and setter and other function you need
}

2.implements Serializable:

public class JsonData implements Serializable{
    private static final long serialVersionUID = 6037391814333931008L;//It should generate by your IDE
    private JsonObject mData;

    //getter and setter and other function you need
}

3.Use Bundle and Intent to sent Serializable data when start Activity

Intent intent = new Intent(Sender.this, Receiver.class); // replace Sender and Receiver with your Activities 
Bundle bundle = new Bundle();
bundle.putSerializable("Your Key", yourDataClass);//replace yourDataClass with your JsonData class
intent.putExtras(bundle);
startActivity(intent);

4.Receive data in other Activity:

Add code in onCreate of this Activity:

Bundle bundle = getIntent().getExtras();

    if (bundle != null) {

        Serializable data = bundle.getSerializable("Your Key");

        if (data != null) {
            JsonData jsonData= (JsonData) data;
            //now you have got your data and do whatever you want.
        }
    }

But, the code in Step 4 should also be placed in onNewIntent if the Activity is singleTask mode. In this condition , you should save Intent in onNewIntent:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    //here to place the code in Step 4 and also place in onCreate function
}

4 Comments

what if i need to send all objects which is inside array to next activity?
You should create a class which contains the data you want to send, and let this class implements Serializable.And there is code to send:Intent intent = new Intent(..., ...); Bundle bundle = new Bundle(); bundle.putSerializable("key", yourDataClass); intent.putExtras(bundle); startActivity(intent);I think here shouldn't write lots of code
@androidlover sure,wait a moment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.