android.os.NetworkOnMainThreadException与android 4.2

这个问题在这里已经有了答案:

  • 我该如何解决android.os.NetworkOnMainThreadException? 48个答案

  • 在setContentView(R.layout.activity_main)之后将代码写入您的MainActivity文件中;

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    

    并将import语句导入到您的java文件中。

    import android.os.StrictMode;
    

    这是正确的方法:

    public class JSONParser extends AsyncTask <String, Void, String>{
    
        static InputStream is = null;
    
    static JSONObject jObj = null;
    static String json = "";
    
    // constructor
    public JSONParser() {
    
    }
    @Override
    protected String doInBackground(String... params) {
    
    
        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet(url);
    
                HttpResponse getResponse = httpClient.execute(httpPost);
               final int statusCode = getResponse.getStatusLine().getStatusCode();
    
               if (statusCode != HttpStatus.SC_OK) { 
                  Log.w(getClass().getSimpleName(), 
                      "Error " + statusCode + " for URL " + url); 
                  return null;
               }
    
               HttpEntity getResponseEntity = getResponse.getEntity();
    
            //HttpResponse httpResponse = httpClient.execute(httpPost);
            //HttpEntity httpEntity = httpResponse.getEntity();
            is = getResponseEntity.getContent();            
    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            Log.d("IO", e.getMessage().toString());
            e.printStackTrace();
    
        }
    
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
    
        // return JSON String
        return jObj;
    
    
    }
    protected void onPostExecute(String page)
    {   
        //onPostExecute
    }   
    }
    

    调用它(从主):

    mJSONParser = new JSONParser();
    mJSONParser.execute();
    

    请确保您不会在UI线程上执行任何网络访问,而是在异步任务中执行此操作

    之所以你的应用程序崩溃在Android 3.0及以上版本,但在Android 2.x上正常工作,是因为HoneyComb对UI线程的滥用要严格得多。 例如,当运行HoneyComb或更高版本的Android设备检测到UI线程上的网络访问时,将引发NetworkOnMainThreadException。

    看到这个

    链接地址: http://www.djcxy.com/p/5017.html

    上一篇: android.os.NetworkOnMainThreadException with android 4.2

    下一篇: Difference between margin and padding?