Http请求通信(工具类)

异步消息处理流程是:

首先需要在主线程当中创建一个Handle对象,并重写handlerMessage()方法。
然后当子线程中需要进行UI操作时,就创建一个Message对象,并通过Handler将这条消息发送出去。
之后这条消息会被添加到MessageQueue的队列中等待被处理,而Loop则会一直尝试从MessageQueue中取出待处理消息,最后分发回HandlerMessage()方法中。
 
 
 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请求通信(工具类)的更多相关文章

  1. Https通信工具类

    记录一个在微信开发中用到的https通信工具类,以后会用到的. 用于https通信的证书信任管理器 import java.security.cert.CertificateException; im ...

  2. HttpUtils 用于进行网络请求的工具类

    原文:http://www.open-open.com/code/view/1437537162631 import java.io.BufferedReader; import java.io.By ...

  3. HTTP请求客户端工具类

    1.maven 引入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId> ...

  4. 发送http请求和https请求的工具类

    package com.haiyisoft.cAssistant.utils; import java.io.IOException;import java.util.ArrayList; impor ...

  5. java并发编程系列原理篇--JDK中的通信工具类Semaphore

    前言 java多线程之间进行通信时,JDK主要提供了以下几种通信工具类.主要有Semaphore.CountDownLatch.CyclicBarrier.exchanger.Phaser这几个通讯类 ...

  6. 分享自己配置的HttpURLConnection请求数据工具类

    >>该工具类传入string类型url返回string类型获取结果import java.io.BufferedReader;import java.io.InputStream;impo ...

  7. java中模拟http(https)请求的工具类

    在java中,特别是java web中,我们经常需要碰到的一个场景是我们需要从服务端去发送http请求,获取到数据,而不是直接从浏览器输入请求网址获得相应.比如我们想访问微信接口,获取其返回信息. 在 ...

  8. 高德地图web端笔记;发送http请求的工具类

    1.查询所有电子围栏 package com.skjd.util; import java.io.BufferedReader; import java.io.InputStream; import ...

  9. httputil用http获取请求的工具类

    package com.xiaocan.demo.util; import java.io.IOException; import java.io.InputStream; import java.u ...

  10. Java httpclent请求httpclentUtils工具类

    第一种写法: import java.io.IOException; import java.io.InterruptedIOException; import java.io.Unsupported ...

随机推荐

  1. Linux修改文件时候出现崩溃,产生了一个.swap交换文件,如何修复?

    有时候在用vim打开文件时提示类似以下的信息: E325: 注意 发现交换文件 ".exportcert.cpp.swp" 所有者: liuchuanliang    日期: Th ...

  2. Jersey+Spring+Maven(转)

    spring和maven的搭建参考相关文档.本文只介绍与jersey有关配置. 一.jersey在maven中的依赖包 <!-- jersey --> <dependency> ...

  3. SourceTree + Bitbucket - 轻松云端作业

    如何新建bitbucket工程 常见流程: 1: 本地电脑 A:创建Xcode工程 2:Bitbucket网站 A:"创建"仓库 B:然后"从零开始",根据提示 ...

  4. MVVM模式应用体会

    转自:http://www.cnblogs.com/626498301/archive/2011/04/08/2009404.html 进公司实习工作后,本人接触的第一个技术名语就是MVVM模式,从学 ...

  5. 迷宫城堡--HDOJ 1269(Tarjan)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. [MarsZ]Unity3d游戏开发之Unity3d全策划配置新手指引

    Unity3d全策划配置新手指引 前言... 2 版本... 2 作者... 2 功能... 2 类型... 2 触发类型... 2 步骤类型... 3 实现... 4 简要... 4 策划方面... ...

  7. 深入设计模式(二)——单例模式(Singleton Pattern)

    一.单例模式介绍 单例模式(Singleton Pattern),保证一个类只有一个实例,并提供一个访问它的全局访问点.单例模式因为Singleton封装它的唯一实例,它就可以严格地控制客户怎样访问它 ...

  8. Raspberry Pi Kernel Compilation 内核编译官方文档

    elinux.org/Raspberry_Pi_Kernel_Compilation#Use_the_provided_compiler Software & Distributions: S ...

  9. poj 3608 Bridge Across Islands

    题目:计算两个不相交凸多边形间的最小距离. 分析:计算几何.凸包.旋转卡壳.分别求出凸包,利用旋转卡壳求出对踵点对,枚举距离即可. 注意:1.利用向量法判断旋转,而不是计算角度:避免精度问题和TLE. ...

  10. 获取toast值

    /** * 获取Toast的String值 * @return */ public String getToast(int timeout){ TextView toastTextView = nul ...