0


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 =newStrictMode.ThreadPolicy.Builder().permitAll().build();StrictMode.setThreadPolicy(policy);}

在这里插入图片描述
仍然出现了, 这种问题报错,未解决;

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

newThread(newRunnable(){@Overridepublicvoidrun(){//请求详情}).start();

以下是之前的方案,直接调用 new HttpHello();

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

packagecom.sun.dyCapture;importandroid.os.Bundle;importorg.json.JSONException;importorg.json.JSONObject;importjava.util.ArrayList;importjava.util.List;importjava.util.Set;importcz.msebera.android.httpclient.NameValuePair;importcz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;importcz.msebera.android.httpclient.client.methods.CloseableHttpResponse;importcz.msebera.android.httpclient.client.methods.HttpPost;importcz.msebera.android.httpclient.impl.client.CloseableHttpClient;importcz.msebera.android.httpclient.impl.client.HttpClients;importcz.msebera.android.httpclient.message.BasicNameValuePair;importcz.msebera.android.httpclient.util.EntityUtils;publicclassHttpHello{publicstaticJSONObjectBuild2Json(Bundle bundle)throwsException{JSONObject json =newJSONObject();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;}publicvoidsendRequestWithHttpConnection(String result)throwsException{// 创建Httpclient对象172CloseableHttpClient httpclient =HttpClients.createDefault();// 创建http POST请求HttpPost httpPost =newHttpPost("ip:端口/Cookie");// 设置2个post参数List<NameValuePair> parameters =newArrayList<NameValuePair>(0);
        parameters.add(newBasicNameValuePair("result", result));// 构造一个form表单式的实体UrlEncodedFormEntity formEntity =newUrlEncodedFormEntity(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)){// 执行请求// 判断返回状态是否为200if(response.getStatusLine().getStatusCode()==200){String content =EntityUtils.toString(response.getEntity(),"UTF-8");System.out.println("内容"+ content);}elseSystem.out.println("内容错误"+ response.getStatusLine().getStatusCode());}
        httpclient.close();}HttpHello(finalString result){//开启线程发起网络连接newThread(newRunnable(){publicvoidrun(){try{sendRequestWithHttpConnection(result);}catch(Exception e){
                    e.printStackTrace();}}}).start();}}
标签: android .net java

本文转载自: https://blog.csdn.net/weixin_38927522/article/details/128219916
版权归原作者 Codeooo 所有, 如有侵权,请联系我们删除。

“Xposed回发android.os.NetworkOnMainThreadException修复”的评论:

还没有评论