【问题标题】:Read JSON with Android [duplicate]使用 Android 读取 JSON [重复]
【发布时间】: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" />

【问题讨论】:

标签: android json exception


【解决方案1】:

尝试使用AsyncTask 执行此操作,因为网络不能在主线程上

【讨论】:

    猜你喜欢
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    相关资源
    最近更新 更多