【发布时间】:2014-01-28 18:08:26
【问题描述】:
我正在尝试从服务器读取 JSON,但没有得到
public JSONObject getJSON(){
JSONObject jsonObject = null;
try{
// Create a new HTTP Client
DefaultHttpClient defaultClient = new DefaultHttpClient();
// Setup the get request
HttpGet httpGetRequest = new HttpGet("URL");
// 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 = new JSONObject(json);
} catch(Exception e){
// In your production code handle any errors and catch the individual exceptions
e.printStackTrace();
}
return jsonObject;
}
例外:
android.os.NetworkOnMainThreadException
我正在使用标签权限在 XML Android 中使用互联网
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
【问题讨论】: