Xposed回发android.os.NetworkOnMainThreadException修复

最近用xposed进行hook回发的时候,又出现了新的问题;

android.os.NetworkOnMainThreadException;

在Android4.0以后,写在主线程(就是Activity)中的HTTP请求,运行时都会报错,这是因为Android在4.0以后为了防止应用的ANR(Aplication
Not Response)异常,Android这个设计是为了防止网络请求时间过长而导致界面假死的情况发生。

我尝试了网上的第一种解决方法:

1.在Activity的onCreate()方法中加入代码

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

Xposed回发android.os.NetworkOnMainThreadException修复_第1张图片
仍然出现了, 这种问题报错,未解决;

2. 尝试启动子线程进行网络请求

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

以下是之前的方案,直接调用 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();
    }

}





你可能感兴趣的:(2022,android,.net,java)