0

在我的代码中,我使用 for 循环获取了一个数组。该数组包含四个值。如何在文本视图中打印值?我想在四个文本视图中打印这四个值。

我的代码:

HttpClient client2 = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client2.getParams(), 15000);
HttpConnectionParams.setSoTimeout(client2.getParams(), 15000);
HttpUriRequest request2 = new HttpGet(SelectMenuAPI2);
HttpResponse response2 = client2.execute(request2);
InputStream atomInputStream2 = response2.getEntity().getContent();
BufferedReader in2 = new BufferedReader(new InputStreamReader(atomInputStream2));
String line2;
String str2 = "";
while ((line2 = in2.readLine()) != null) {
    str2 += line2;
}

JSONObject json3 = new JSONObject(str2);
//  message = json2.getString("message");
status = json3.getString("status");
if (status.equals("1")) {
    JSONArray school2 = json3.getJSONArray("data");
    for (int i = 0; i < school2.length(); i++) {
        JSONObject object3 = school2.getJSONObject(i);
        Elementarytxt.settext("");
        Middle.setText("");
        High.setText("");
        Atypical.setText("");
    }
}

我的 JSON 数据:

{
 "status":1,
 "data":[
         {"title":"Elementary"}, 
         {"title":"Middle"},
         {"title":"High"},
         {"title":"Atypical"}
        ]
}
4

3 回答 3

1
JSONArray jsonArray = jObject.getJSONArray(ARRAYNAME);
            if (jObject != null) {
                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                  txt.setttext(jsonObject.getString(CREATEDDATE));
                  txt.setttext(jsonObject.getString(SUBJECT));


                }
            }
于 2013-08-22T06:22:01.283 回答
0

请检查这个。它可能会帮助你...

String [] mVal = new String[school2.length()];

for (int i = 0; i < school2.length(); i++) 
    {
         mVal[i] =school2.getJSONObject(i).getString("title");       
    }
Elementarytxt.setText(mVal[0]);
Middle.setText(mVal[1]);
High.setText(mVal[2]);
Atypical.setText(mVal[3]);
于 2013-08-22T06:30:59.593 回答
0

我希望这能帮到您..

String[] vals=new String[4];
    for (int i = 0; i < school2.length(); i++) {
                    JSONObject object3 = school2.getJSONObject(i);
    vals[i]=object3.getString("title");                    

                }
     Elementarytxt.settext(vals[0]);
                                                  Middle.setText(vals[1]);
                                                 High.setText(vals[2]);
                                              Atypical.setText(vals[3]);
于 2013-08-22T06:31:31.143 回答