1

我正在构建一个应用程序,在其中我以["anything","everthing"]这种格式使用服务器端 JSON 数据。我正在尝试将两个字符串存储到不同的不同变量中,我尝试了这些代码:

try {
        URL API = new URL(
                "http://......com/asd.php");
        URLConnection tc = API.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                tc.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            JSONArray ja = new JSONArray(line);
            for (int i = 0; i < ja.length(); i++) {
                Log.i(i);
                Log.i("JSONArray String here " + ja.toString());
            }
        }
    } catch (MalformedURLException 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();
    }

我要一串串出来。谁能帮我将这两个字符串存储在不同的变量中。

4

1 回答 1

1

将您的代码更改为:

BufferedReader in = new BufferedReader(new InputStreamReader(
                tc.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
    while ((line = in.readLine()) != null) {
        sb.append(line);
    }
    in.close();
JSONArray ja = new JSONArray(sb.toString());
for (int i = 0; i < ja.length(); i++) {
    Log.i(i);
    Log.i("JSONArray String here " + ja.getString(i));
 }
//Your Code...
于 2012-11-20T18:37:11.213 回答