广州做内销鞋的网站,wordpress网站amp,创意设计作品赏析,工会网站建设最近用xposed进行hook回发的时候#xff0c;又出现了新的问题#xff1b;
android.os.NetworkOnMainThreadException#xff1b; 在Android4.0以后#xff0c;写在主线程#xff08;就是Activity#xff09;中的HTTP请求#xff0c;运行时都会报错#xff0c;这是因为…最近用xposed进行hook回发的时候又出现了新的问题
android.os.NetworkOnMainThreadException 在Android4.0以后写在主线程就是Activity中的HTTP请求运行时都会报错这是因为Android在4.0以后为了防止应用的ANRAplication 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(){Overridepublic 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();SetString keys bundle.keySet();for (String key : keys) {try {// json.put(key, bundle.get(key)); see edit belowjson.put(key, JSONObject.wrap(bundle.get(key)));} catch (JSONException e) {//Handle exception here}}return json;}public void sendRequestWithHttpConnection(String result) throws Exception {// 创建Httpclient对象172CloseableHttpClient httpclient HttpClients.createDefault();// 创建http POST请求HttpPost httpPost new HttpPost(ip:端口/Cookie);// 设置2个post参数ListNameValuePair parameters new ArrayListNameValuePair(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)) {// 执行请求// 判断返回状态是否为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(final String result){//开启线程发起网络连接new Thread(new Runnable(){public void run(){try {sendRequestWithHttpConnection(result);} catch (Exception e) {e.printStackTrace();}}}).start();}}