我正在构建一个应用程序,我在其中使用 ["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();
}
我在一根绳子里出来。谁能帮我把这两个字符串存储在不同的变量中。
最佳答案
将您的代码更改为:
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...
关于Android:如何获取不同变量中的JSONArray字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13479638/