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 ...
随机推荐
- Oracle core03_ACID
ACID特性 oracle如何使用undo和redo来保证了关系数据库的ACID特性. ACID的特性简单描述为: Atomic:以事务为单位的原子性 Consistency:保证数据一致性 Isol ...
- C# winform 导出导入Excel/Doc 完整实例教程[网上看到的]
还真没做过winform的导出导入,今天上网百度了一下.结果--- 所以还是我自己写个吧.之前做过web的,半搬半做就OK. 1添加引用:Aspose.Cells.dll(我们就叫工具包吧,可以从网上 ...
- 比较详细的利用虚拟机对SD卡FAT32+EXT4+Ext4分区图解教程
如果大家嫌虚拟机复杂,我这里提供一个我没用虚拟机之前的分区方法:这个方法实际是可行的 我在没有用虚拟机之前,我是这样操作的1.首先在分区软件分好fat32+ext2+ext22.然后用recovery ...
- KTV点歌系统播放原理
北大青鸟KTV点歌系统播放原理 指导老师--原玉明 1.首先知道有两个类(PlayList Song) PlayList类中有一方法 public static bool AddSong(Song ...
- JavaScript高级程序设计44.pdf
unload事件 与load事件对应的是unload事件,这个事件在文档被完全卸载后触发,只要用户从一个页面切换到另一个页面,就会发生unload事件,最多的情况是清除引用,避免内存泄漏 与load事 ...
- Oracle 11gR2 create init script
设置oracle用户变量. [oracle@db01 ~]$ vi /etc/oratab # end line: change db01:/oracle/app/product/11.2.0/db_ ...
- ARC __bridge modifiers demystified
http://stackoverflow.com/questions/14207960/arc-bridge-modifiers-demystified Because I learned what ...
- puppet任务计划
- linux内核--页高速缓存
页高速缓存,可以理解为对磁盘中的文件内容进行缓存的一种缓存策略,当然它不仅仅用于磁盘文件. 当对同一磁盘数据反复访问时,缓存数据就是非常必须的了.这就是buffer和 cache这两个概念中的buff ...
- 使用js实现html锚功能,可以任意定位锚的位置,比锚更加灵活
今天记录一个js功能,发现该功能很实用,拿出来跟大家分享. 这里我使用js的window.scrollTo()函数,该函数的用法给大家重复一下,语法为scrollTo(x,y),这里x表示在窗口文档显 ...