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 ...
随机推荐
- bzoj1016
这道题主要利用了最小生成树的两个性质 最小生成树每种边权的数目固定不变 最小生成树每种边权带来的连通状况一定唯一 由于每种边权的只有不到10种,所以直接穷举然后乘法原理即可 ; type node=r ...
- [FJSC2014]滑行
[题目描述] 首长NOI惨跪,于是去念文化课了.现在,他面对一道物理题. 现在有一个小滑块可以在地面上滑行,地面上被划分成不同的区域,使得小滑块在不同的区域内部有一个不同的速度上限. 小滑块在(0,0 ...
- [NYOJ 860] 又见01背包
又见01背包 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W 的物品,求所 ...
- (转载)puremvc框架之proxy
(转载)http://www.cnblogs.com/yjmyzz/archive/2010/08/01/1789769.html 上一篇 puremvc框架之Command 里,已经学习了如何利用C ...
- Devexpress 之gridControl
1.gridControl如何去掉主面板? 鼠标右键Run Designer=>OptionsView => ShowGroupPanel=False: 2.gridControl如何设置 ...
- tomcat web容器中,调用jersey client端报错的处理
在web工程中,写main方法,运行ok. 发布到tomcat中后,报错. javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/r ...
- HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)
Problem Description In millions of newspapers across the United States there is a word game called J ...
- 《锋利的Jquery第二版》读书笔记 第三章
DOM操作的分类 1.DOM Core不专属JavaScript,任何一种支持DOM的程序设计语言都可以使用它,也可以处理XML等标记语言编写出来的文档,getElementById().setAtt ...
- YII 表单验证规则
官方文档:http://www.yiichina.com/guide/form.model 类参考手册:http://www.yiichina.com/api/CValidatorhttp://www ...
- hdoj 1035 Robot Motion
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...