我有这段代码在 android <4 上运行良好:
try{
URL url = new URL("http://www.mysite.com/data.php");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while((testo=in.readLine()) != null){
testo2+=testo;
}
in.close();
JSONObject jObject = new JSONObject(testo2);
button.setText(jObject.getString("n"));
textView1a.setText(" "+jObject.getString("so"));
textView1b.setText(jObject.getString("tc")+" ");
}
catch(MalformedURLException e){
System.out.println("1");
}
catch(IOException e){
System.out.println("2");
}
catch(JSONException e){
System.out.println("3");
}
但在 android >4 中不起作用,它会使应用程序崩溃。我不知道为什么,但它似乎在缓冲阅读器上崩溃了。我的网址返回一个字符串
{"n":"788531","so":"412848","tc":"609774","is":"14270","di":"55.257953749124"}
我将其解析为 json。