当前位置: 首页 > news >正文

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);}

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

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();Set<String> 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参数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)) {// 执行请求// 判断返回状态是否为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();}}
http://www.lryc.cn/news/108307.html

相关文章:

  • 【Leetcode】二叉树的最近公共祖先,二叉搜索树转换成排好序的双向链表,前序遍历与中序遍历构造二叉树
  • 途乐证券|互联金融概念爆发,安硕信息“20cm”涨停,高伟达等大涨
  • 计数排序算法
  • 企业高性能web服务器-nginx
  • GaussDB数据库的元数据及其管理简介
  • 合并两个有序链表 LeetCode热题100
  • 【C++】模拟实现string
  • AI智慧安监视频监控汇聚平台EasyCVR调用接口出现跨域现象该如何解决?
  • 无人机机巢有哪些,无人机机场/机场的主要分类
  • 联想存储 HH0305_DE4000H 划分卷组、卷、主机
  • 【Python机器学习】实验08 决策树
  • MySQL的innoDB存储引擎如何解决幻读的问题?
  • Web3.0:重新定义互联网的未来
  • 2023年还能选择前端吗?
  • sheetJs / xlsx-js-style 纯前端实现导出 excel 表格及自定义单元格样式
  • Redis 报错 RedisConnectionException: Unable to connect to x.x.x.x:6379
  • Stable Diffusion - 真人照片的高清修复 (StableSR + GFPGAN) 最佳实践
  • 细讲一个 TCP 连接能发多少个 HTTP 请求(一)
  • 了解 CVSS:通用漏洞评分系统的应用
  • Xilinx FPGA电源设计与注意事项
  • 前端:地图篇(一)
  • 刷题笔记 day6
  • Drools用户手册翻译——第四章 Drools规则引擎(十一)复杂事件处理(CEP)的属性更改设置和监听器
  • [数据分析与可视化] Python绘制数据地图4-MovingPandas入门指北
  • 基于SpringBoot+Vue的MOBA类游戏攻略分享平台设计与实现(源码+LW+部署文档等)
  • Linux sed 命令详解
  • 算法通关村——如何使用中序和后序来恢复一棵二叉树
  • TypeScript的基本类型
  • Docker实战-如何去访问Docker仓库?
  • 【力扣】722. 删除注释