【发布时间】:2013-09-24 17:47:53
【问题描述】:
我创建了一个从服务器提取数据的 jsonobject,但它仅适用于小于 3 的 android 版本(旧版本)。让它在所有版本上工作的代码是什么??
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DefaultHttpClient client;
client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://saurabhgoyal.comoj.com/json_test.php");
HttpResponse r;
try {
r = client.execute(get);
HttpEntity e = r.getEntity();
InputStream is=e.getContent();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader reader=new BufferedReader(isr);
String results=reader.readLine();
Toast.makeText(this, results, Toast.LENGTH_SHORT).show();
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
Toast.makeText(this, "failure", Toast.LENGTH_SHORT).show();
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
【问题讨论】:
-
您能否提一下在 3+ 版本上运行时究竟发生了什么?任何错误消息或日志都会有所帮助
-
我在我的牢房里试过它直接崩溃说不幸的是它已经停止工作了。
-
应用崩溃时请打开Logcat,查找错误日志,如果可能的话粘贴到这里。
-
在 3.0 及以上版本中使用异步任务。
标签: java android html json apache