Java中的以下android应用程序代码输出到:
[{"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}]}
从codeforces api- http://codeforces.com/api/user.info?handles=DmitriyH;
中提取数据时但我只想要用户的“firstName”。 任何人都可以推荐更改我的代码吗?
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);
}
}
}
答案 0 :(得分:1)
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;
}
}
修改强>
现在假设我正在使用网址&#34; codeforces.com/api/user.status?handle = avm12&#34 ;;我想提取前十个(或者说n个数字)&#34;问题&#34;标签。那我该怎么办?
首先获取根JSONArray
JSONArray array= new JSONObject(data).getJSONArray("result");
然后在for循环中获取所有需要的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...
}
考虑将&count=N
添加到您的网址,以将输出限制为所需的N个结果......
答案 1 :(得分:0)
您的完整JSONObject
包含一个标记为"result"
的数组,因此您应该像这样提取firstName:
JSONArray result = json.getJSONArray("result");
JSONObject jo = result.getJSONObject(0);
String firstName = jo.getString("firstName");
答案 2 :(得分:0)
您的第一个代码是JSONArray
,因此您需要制作一个返回JSONArray
而不是JSONObject
的方法。根据您的要求,您可以通过
jArray.getJSONObject(0).get("firstName");
答案 3 :(得分:0)
在您发布的代码中未使用正确的网址。 以下作品:
final static String URL = http://codeforces.com/api/user.info?handles=;
...
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = lastSub("DmitriyH");
final JSONArray result = json.getJSONArray("result");
final JSONObject jsonObject = result.getJSONObject(0);
return jsonObject.getString("firstName");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
....