我正在构建一个应用程序,我以["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();
}
我在单个字符串中出来了。任何人都可以帮助我将这两个字符串存储在不同的变量中。
答案 0 :(得分: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...