最近用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);
}
仍然出现了, 这种问题报错,未解决;
2. 尝试启动子线程进行网络请求
new Thread(new Runnable(){
@Override
public void run() {
//请求详情
}).start();
以下是之前的方案,直接调用 new HttpHello();文章来源:https://www.toymoban.com/news/detail-522153.html
https://codeooo.blog.csdn.net/article/details/126099666文章来源地址https://www.toymoban.com/news/detail-522153.html
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();
}
}
到了这里,关于Xposed回发android.os.NetworkOnMainThreadException修复的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!