0

First I need to check the json object status 400 if status 400 to get the image otherwise it shows error. I don't how to check status in JSON empty object in android volley. and also I need some help on how to get image by using banner path and banner name.

My Json

  {
    "status": "200",
    "requestType": "bannerImages",
    "basePath": "http:\/\/192.168.0.33\/cartwebsite3\/",
    "bannerPath": "http:\/\/192.168.0.33\/cartwebsite3\/cdn-images\/banner\/",
    "response": {
        "data": [
            {
                "banner_id": "37",
                "banner_name": "1457324300894ac3df08bd3648452703d8412f82c2.jpg",
                "banner_link": "",
                "banner_blocked": "0",
                "banner_created": "admin",
                "created_on": "1457324300"
            },
            {
                "banner_id": "36",
                "banner_name": "14573242986be953c24858a3c2d787d57e6b77be1f.jpg",
                "banner_link": "",
                "banner_blocked": "0",
                "banner_created": "admin",
                "created_on": "1457324298"
            },
            {
                "banner_id": "35",
                "banner_name": "1457324295f8d8153fb4f29d3af15276db22435d48.jpg",
                "banner_link": "",
                "banner_blocked": "0",
                "banner_created": "admin",
                "created_on": "1457324295"
            }
        ]
    },
    "request": {
        "postData": [],
        "getData": {
            "type": "bannerImages",
            "result": "json"
        }
    }
}

Thank in Advance

Pooya
  • 6,083
  • 3
  • 23
  • 43
kani mozhi
  • 23
  • 1
  • 10
  • Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Rohit Arya Apr 22 '16 at 05:04
  • I wanna check status 400 or not how to check that status – kani mozhi Apr 22 '16 at 05:10
  • refer my answer : http://stackoverflow.com/questions/36385467/json-parsing-in-android-studio-1-5-1?answertab=votes#tab-top – ErShani Apr 22 '16 at 05:14
  • okay, try like [this answer](http://stackoverflow.com/questions/36785236/how-to-check-the-status-in-json-object-to-get-the-image-from-array-of-object-in/36785360#36785360) – Rohit Arya Apr 22 '16 at 05:15

3 Answers3

0

If you just want to get status, try like this:

JSONObject jsonObject = new JSONObject(jsonString);
String status = jsonObject.optString("status");

if you want to parse whole data, I would recommend you to use Gson.

Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
0

For easy JSON parsing/manipulating/etc use GSON library at android. Example you can make custom objects and straight create new instance of your custom object from parsing and then just check fields or whatever you need to do.

CustomObject object= gson.fromJson(JSON, CustomObject.class);
if(object.bannerPath != null) {
  //Do something
}

Here is the GSON in Github

and here for GSON tutorial

Leolian
  • 190
  • 1
  • 7
0

//editing Rohit Arya Code little bit

JSONObject jsonObject = new JSONObject(jsonString);
String status = jsonObject.optString("status");
if(status.equalIgnorecase("400")){
    try {
                    JSONObject jsonObject=new JSONObject("yourResponse");
                   jsonObject1=jsonObject.getJSONObject("response");
                    JSONArray jsonArray=jsonObject1.getJSONArray("data");
                    JSONObject jsonObject2;
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject c = jsonArray.getJSONObject(i);
                       String banner_name=c.getString("banner_name");
                        Log.e("banner_name",banner_name);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
}else{
// don't download
}
Amit Ranjan
  • 567
  • 2
  • 11
  • I got output. Thanks a lot Amit.I checked the status like what you written here. – kani mozhi Apr 22 '16 at 07:15
  • Yw :) if this solution helped you accept the answer :) so that when other person face same problem then it will directly see the correct answer – Amit Ranjan Apr 22 '16 at 08:43