【发布时间】:2013-12-16 13:28:37
【问题描述】:
我是 android 世界的新手,我正在尝试从以下 URL 检索 JSONArray:http://www.softmarketing.it/json/view/azienda/7
TabHostExample.java:
public class TabHostExample extends TabActivity {
private static String url = "http://www.softmarketing.it/json/view/azienda/7";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_host_example);
//JSONParser parser= new JSONParser();
//JSONObject myJSON= parser.makeHttpRequest(url, "POST", null);
try{
// Create a new HTTP Client
DefaultHttpClient defaultClient = new DefaultHttpClient();
// Setup the get request
HttpGet httpGetRequest = new HttpGet("http://www.softmarketing.it/json/view/azienda/7");
// Execute the request in the client
HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
// Grab the response
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
// Instantiate a JSON object from the request response
JSONObject jsonObject = new JSONObject(json);
} catch(Exception e){
// In your production code handle any errors and catch the individual exceptions
e.printStackTrace();
}
.....
在 LogCat 中我看到了这个:
12-16 08:14:41.987: E/MYAPP(1183): exception: null
12-16 08:21:34.927: E/MYAPP(1245): exception: null
变量 e 具有以下值:
e: 原因:NetworkOnMainThreadEception
还有其他价值观...
你能帮帮我吗?我已经三天了,我正在尝试解决解析...谢谢
【问题讨论】:
-
您正在主线程上执行网络连接。这在 Android 中是不允许的。您必须使用 AsyncTask。 stackoverflow.com/questions/6343166/…
-
你能解释一下我与一个例子有什么关系吗?
-
网上有很多教程。我相信你会找到一个 =)
标签: java android arrays jsonobject