user1788736
user1788736

Reputation: 2845

How to get a specific json key value from post request response in Android?

I have following response from post request using HttpURLConnection:

Post Request Response:

{
    "LatestData": [{
        "ExtraData": null,
        "ID": 0,
        "season": false,
        "latest": 0,
        "url": "http://www.awebsite.com/images/12.jpg"
    }]
}

How to get value of URL? I tried following but Android Studio keeps giving me error:

String newURL = sb.getJSONObject("LatestData").getString("url");
String newURL = sb.getJSONArray("LatestData").getJSONObject(0).getString("url");

Android Studio Error:

error: cannot find symbol method getJSONObject(String)
error: cannot find symbol method getJSONArray(String)

could you guys help me obtain the value of url and let me know what libraries i need to import to android studio so getsonObject works ?Thanks

android code:

 if (myURLConnection.getResponseCode() == 200) {
                br = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream(), "utf-8"));
                while (true) {
                    line = br.readLine();
                    if (line != null) {
                        sb.append(line + "\n");


                        //String newURL = sb.getJSONObject("LatestData").getString("url");
                        String newURL =sb.getJSONArray("LatestData").getJSONObject(0).getString("url");
                        mWebView.loadUrl("javascript:MyFunction('" +newURL + "');");
                    } else {
                        br.close();
                        return sb.toString();
                    }
                }
            }

Upvotes: 1

Views: 4367

Answers (6)

Rohit Rawat
Rohit Rawat

Reputation: 453

use jsonschema2pojo to generate pojo classes for a JSON data

Upvotes: 0

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

You need to convert sb to a JSONObject to access properties:

JSONOjbect jsonResponse = new JSONObject(new String(sb));

and then:

try {
    JSONArray jsonArray = jsonResponse.getJSONArray("LatestData");
    if (jsonArray != null && jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject dataOjbect = jsonArray.getJSONObject(i);
            String url = dataOjbect.getString("url");
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

Upvotes: 2

Mohammed Gomaa
Mohammed Gomaa

Reputation: 91

Can you try this :

StringBuilder sb = new StringBuilder();
line = br.readLine();
while(line != null){
    sb.append(line);
}
// if it's fail here then this means there is an issue in parsing JSONObject
JSONObject jsonObj = new JSONObject(sb.toString());        
JSONArray latestData = jsonObj.getJSONArray("LatestData");
JSONObject jsonObject = latestData.getJSONObject(0);
String url = jsonObject.getString("url");

Also I'm highly recommending to use Retrofit with GSON .. you can follow this article : http://www.vogella.com/tutorials/Retrofit/article.html

Upvotes: 0

Kenry Sanchez
Kenry Sanchez

Reputation: 1743

Try it using loadJSONArray

String url = loadJSONArray(sb)..getJSONObject(0).getString("url");

Upvotes: 1

vikas kumar
vikas kumar

Reputation: 11018

Your approach should be like this.

try {
        List<String> urlist = new ArrayList<>();
        JSONObject jsonObject = new JSONObject(sb.toString());
        JSONArray jsonArray =  jsonObject.getJSONArray("LatestData");
        for (int count = 0; count<jsonArray.length(); count++){
            JSONObject jsonInner =  jsonArray.getJSONObject(count);
            String url = jsonInner.getString("url");
            //store your url in some list
            urlist.add(url);
        }

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

Upvotes: 0

Pavya
Pavya

Reputation: 6025

try {
            JSONObject jsonObject = new JSONObject(sb);
            String url = jsonObject.optJSONArray("LatestData").getJSONObject(0).optString("url");
        } catch (JSONException e) {
            e.printStackTrace();
        }

Upvotes: 1

Related Questions