Xposed postback android.os.NetworkOnMainThreadException fix

Recently, when using xposed for hook postback, a new problem appeared;

android.os.NetworkOnMainThreadException;

After Android 4.0, the HTTP request written in the main thread (that is, Activity) will report an error at runtime. This is because after Android 4.0, in order to prevent the ANR (Aplication Not Response) exception of the application, the design of Android is to prevent the network request from taking too long to cause the interface to freeze
.

I tried the first solution from the web:

1. Add code to Activity's onCreate() method

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

insert image description here
It still occurs, this kind of problem reports an error and has not been resolved;

2. Try to start a child thread to make a network request

    new Thread(new Runnable(){
    
    
        @Override
        public void run() {
    
    
           //请求详情
        }).start();

The following is the previous scheme, directly calling new HttpHello();

https://codeooo.blog.csdn.net/article/details/126099666

package com.sun.dyCapture;

import android.os.Bundle;

import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
import cz.msebera.android.httpclient.client.methods.CloseableHttpResponse;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.impl.client.CloseableHttpClient;
import cz.msebera.android.httpclient.impl.client.HttpClients;
import cz.msebera.android.httpclient.message.BasicNameValuePair;
import cz.msebera.android.httpclient.util.EntityUtils;




public class HttpHello {
    
    


    public static JSONObject Build2Json(Bundle bundle) throws Exception {
    
    
        JSONObject json = new JSONObject();
        Set<String> keys = bundle.keySet();
        for (String key : keys) {
    
    
            try {
    
    
                // json.put(key, bundle.get(key)); see edit below
                json.put(key, JSONObject.wrap(bundle.get(key)));
            } catch (JSONException e) {
    
    
                //Handle exception here
            }
        }
        return json;
    }


    public void sendRequestWithHttpConnection(String result) throws Exception {
    
    

        // 创建Httpclient对象172
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建http POST请求
        HttpPost httpPost = new HttpPost("ip:端口/Cookie");
        // 设置2个post参数
        List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
        parameters.add(new BasicNameValuePair("result", result));
        // 构造一个form表单式的实体
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
        // 将请求实体设置到httpPost对象中
        httpPost.setEntity(formEntity);
        //伪装浏览器
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36");

        try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
    
    
            // 执行请求
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
    
    
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println("内容" + content);
            } else
                System.out.println("内容错误" + response.getStatusLine().getStatusCode());
        }
        httpclient.close();
    }

    HttpHello(final String result){
    
    
        //开启线程发起网络连接
        new Thread(new Runnable(){
    
    
            public void run(){
    
    
                try {
    
    
                    sendRequestWithHttpConnection(result);
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }
            }
        }).start();
    }

}





Guess you like

Origin blog.csdn.net/qq_41369057/article/details/131323530