JSONObject return type
36,498
public JSONObject lastSub(String username) throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(username);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
//httpStuff.setText("xxx");
int status = r.getStatusLine().getStatusCode();
if(status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject last = new JSONObject(data).getJSONArray("result").getJSONObject(0);
return last;
}
else {
Toast.makeText(Http.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
EDIT
Now suppose I am using the url "codeforces.com/api/user.status?handle=avm12"; and I want to extract the first ten( or say n number of them) "problem" tags. What should I do then?
Well first get the root JSONArray
JSONArray array= new JSONObject(data).getJSONArray("result");
Then in a for loop get all the needed JSONObjects
for(int k=0;k<n;k++){ // n must be less than array.length()
JSONObject problemObject=array.getJSONObject(k).getJSONObject("problem");
//do what you need to do with the problemObject E.G. Add them to an ArryList...
}
Consider adding &count=N
to your url to limit the output to the needed N results...
Author by
Avinash
Updated on May 23, 2020Comments
-
Avinash about 4 years
The following android application code in Java outputs to:
[{"handle":"DmitriyH","firstName":"Dmitriy","lastName":"Khodyrev","country":"Russia","city":"Moscow","organization":"KL","contribution":122,"rank":"master","rating":2040,"maxRank":"international master","maxRating":2072,"lastOnlineTimeSeconds":1432130513,"registrationTimeSeconds":1268570311}]}
when extracting data from codeforces api- http://codeforces.com/api/user.info?handles=DmitriyH;
but I only want "firstName" of the user. Can anyone recommend changes to my code??
public class Http extends Activity { TextView httpStuff; HttpClient client; JSONObject json; final static String URL = http://codeforces.com/api/user.info?handles=; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.httpex); httpStuff = (TextView)findViewById(R.id.tvHttp); client = new DefaultHttpClient(); new Read().execute("result"); } public JSONObject lastSub(String username) throws ClientProtocolException, IOException, JSONException { StringBuilder url = new StringBuilder(URL); url.append(username); HttpGet get = new HttpGet(url.toString()); HttpResponse r = client.execute(get); //httpStuff.setText("xxx"); int status = r.getStatusLine().getStatusCode(); if(status == 200) { HttpEntity e = r.getEntity(); String data = EntityUtils.toString(e); JSONObject last = new JSONObject(data); return last; } else { Toast.makeText(Http.this, "error", Toast.LENGTH_SHORT); return null; } } public class Read extends AsyncTask <String, Integer, String> { @Override protected String doInBackground(String... arg0) { // TODO Auto-generated method stub try { json = lastSub("avm12"); return json.getString(arg0[0]); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub httpStuff.setText(result); } }
}
-
Avinash about 9 yearsPlease explain the logic behind it.
-
n1nsa1d00 about 9 years@Avinash supposing that
arg0[0]
parameter in this linejson.getString(arg0[0]);
is probably"firstName"
and sice you asked for changes to your code I simply changed this lineJSONObject last = new JSONObject(data);
in yourlastSub
method to thisJSONObject last = new JSONObject(data).getJSONArray("result").getJSONObject(0);
-
Flaxie about 9 yearsyeah but you used "user.status" instead of "user.info". "user.status" does not contain the field "firstName"
-
Avinash about 9 yearsThanks a lot. I got it. This way we extract one of the several data. Now suppose I am using the url "codeforces.com/api/user.status?handle=avm12" and I want to extract the first ten( or say n number of them) "problem" tags. What should I do then?
-
Avinash about 9 yearsThanx a lot marco. It was a great help. However, I got into another problem. Now that I have retrieved these data, I want to have http links behind them, (I mean make them clickable so that they open up a web page on being clicked). If you may help here. Thank you.
-
n1nsa1d00 about 9 yearsAre you inflating any layout to display this data in your activity UI ?
-
Avinash about 9 yearsNope. I was trying to do it using Array Adapter.
-
n1nsa1d00 about 9 yearsUnless your're using a
ListView
there's no need to useArrayAdapter
.. Now do this : mark my answer as accepted and create a new question asking how to inflate a layout from a certain json response... and I will make sure to answer you with my solution otherwise someone else might do it it doesn't matter....