Http请求通信(工具类)
Http请求通信(工具类)
异步消息处理流程是:
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
String response = (String) msg.obj;
switch (msg.what) {//根据收到的消息的what类型处理
case ###:
break;
default:
super.handleMessage(msg);//这里最好对不需要或者不关心的消息抛给父类,避免丢失消息
break;
}
}
sendHttpRequest方法有两个参数 1.请求地址 2.请求Json字符串。
HttpUtil.sendHttpRequest(请求地址, 请求Json),
new HttpCallbackListener() { @Override
public void onFinish(String response) {
// 返回内容执行具体的逻辑
Message message = new Message();
message.what = ###;
message.obj = response;
handler.sendMessage(message);
} @Override
public void onError(Exception e) {
// 异常情况进行处理
Toast.makeText(mContext, e.getMessage(), 0).show();
} });
Http请求通信(工具类):
public class HttpUtil {
public interface HttpCallbackListener {
void onFinish(String response);
void onError(Exception e);
}
public static void sendHttpRequest(final String urlStr,
final String jsonStr, final HttpCallbackListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
byte[] json = jsonStr.getBytes();
URL url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setConnectTimeout(5000);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
String.valueOf(json.length));
connection.getOutputStream().write(json); // 通过输出流把数据写到服务器
// if (connection.getResponseCode() == 200) {
// 服务器返回的数据
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = in.readLine()) != null) {
response.append(line);
}
if (listener != null) {
listener.onFinish(response.toString());
}
// }
} catch (Exception e) {
if (listener != null) {
listener.onError(e);
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
}
Http请求通信(工具类)的更多相关文章
- Https通信工具类
记录一个在微信开发中用到的https通信工具类,以后会用到的. 用于https通信的证书信任管理器 import java.security.cert.CertificateException; im ...
- HttpUtils 用于进行网络请求的工具类
原文:http://www.open-open.com/code/view/1437537162631 import java.io.BufferedReader; import java.io.By ...
- HTTP请求客户端工具类
1.maven 引入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId> ...
- 发送http请求和https请求的工具类
package com.haiyisoft.cAssistant.utils; import java.io.IOException;import java.util.ArrayList; impor ...
- java并发编程系列原理篇--JDK中的通信工具类Semaphore
前言 java多线程之间进行通信时,JDK主要提供了以下几种通信工具类.主要有Semaphore.CountDownLatch.CyclicBarrier.exchanger.Phaser这几个通讯类 ...
- 分享自己配置的HttpURLConnection请求数据工具类
>>该工具类传入string类型url返回string类型获取结果import java.io.BufferedReader;import java.io.InputStream;impo ...
- java中模拟http(https)请求的工具类
在java中,特别是java web中,我们经常需要碰到的一个场景是我们需要从服务端去发送http请求,获取到数据,而不是直接从浏览器输入请求网址获得相应.比如我们想访问微信接口,获取其返回信息. 在 ...
- 高德地图web端笔记;发送http请求的工具类
1.查询所有电子围栏 package com.skjd.util; import java.io.BufferedReader; import java.io.InputStream; import ...
- httputil用http获取请求的工具类
package com.xiaocan.demo.util; import java.io.IOException; import java.io.InputStream; import java.u ...
- Java httpclent请求httpclentUtils工具类
第一种写法: import java.io.IOException; import java.io.InterruptedIOException; import java.io.Unsupported ...
随机推荐
- windows桌面添加右键环境
1.组合键win + R,输入regedit,回车 打开注册表编辑器 2.找到目录中[HKEY_CLASSES_ROOT\Directory\Background\shell]对其右键,新建一个项 ...
- oracle稳定执行计划1
稳定执行计划 1 策略: Oracle的sql 执行计划在一些场景下会发生变化,导致系统会发生不可知的情况,影响系统的稳定性,特别是关键业务的sql. 比如下面的场景: 统计信息过老,重新收集了统计信 ...
- [ZOJ 3631] Watashi's BG
Watashi's BG Time Limit: 3 Seconds Memory Limit: 65536 KB Watashi is the couch of ZJU-ICPC Team ...
- active directory 学习和概念整理
第一,在局域网内,如何管理计算机上的资源,需要一个管理策略. 微软提供了两种:工作组和域.两者区别就是,工作组是自治的,组内的计算机个个都作为独立.对等的自治实体而存在.恩,这也是以太网的设计初衷. ...
- vi find和grep
linux grep和find命令 linux中强大且常用命令:find.grep 源码搜索:find . -name "*.xml" | xargs grep -Hna &quo ...
- [CODEVS1014]装箱问题
题目描述 Description 有一个箱子容量为V(正整数,0<=V<=20000),同时有n个物品(0<n<=30),每个物品有一个体积(正整数). 要求n个物品中,任取若 ...
- .Net 调式案例—实验4 高CPU(High CPU)回顾
原文地址:http://blog.csdn.net/directionofear/article/details/8033506 如果Web应用程序经常遇到的问题按频率排名的话,我觉得 第一名unha ...
- Cogs 1709. [SPOJ705]不同的子串 后缀数组
题目:http://cojs.tk/cogs/problem/problem.php?pid=1709 1709. [SPOJ705]不同的子串 ★★ 输入文件:subst1.in 输出文件: ...
- sublime text 2 运行 python时返回EOFError: EOF when reading a line错误
其主要原因是sublime text 2中python没有与 stdin(标准输入)连接所致,解决方法也很简单,那就是安装sublimeREPL插件,然后 Tools->sublimerepl- ...
- 兼容的placeholder属性
作为一个.net后台开发的程序猿,博客里既然大多都是前端相关的博文.是不是该考虑换方向了,转前端开发得了 ... 小小吐槽一下,近期受该不该跳槽所困惑,我有选择困难症! 继续前端,这次说一下输入框 p ...