1.实现自己的请求工具类
/**
* 我的get请求方式工具类
*
* </BR> </BR>
* By:苦涩 </BR>
* 联系作者:QQ 534429149
* */
public class MyGet {
public String doGet(String url) throws ClientProtocolException, IOException{
String result = null;//我们的网络交互返回值
HttpGet myGet = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setIntParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, 5*1000);
httpClient.getParams().setIntParameter(
HttpConnectionParams.SO_TIMEOUT, 30*1000);
HttpResponse httpResponse = httpClient.execute(myGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
result = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
}
return result;
}
}
/**
* 我的post请求方式工具类
*
* </BR> </BR> By:苦涩 </BR> 联系作者:QQ 534429149
* */
public class MyPost {
public String doPost(String url, String mycode, String value) {
String result = null;
HttpResponse httpResponse = null;
HttpPost post = new HttpPost(Model.HTTPURL + url);
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT,
30000); // 超时设置
client.getParams().setIntParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, 10000);// 连接超时
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// Json字符串拼接
nameValuePairs.add(new BasicNameValuePair("mycode", mycode));
nameValuePairs.add(new BasicNameValuePair("value", value));
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
httpResponse = client.execute(post);
Log.e("HTTP", "CODE" + httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils
.toString(httpResponse.getEntity(), "utf-8");
Log.e("HTTP", "result:" + result);
} else {
result = null;
}
} catch (UnsupportedEncodingException e) {
result = null;
} catch (ClientProtocolException e) {
result = null;
} catch (IOException e) {
result = null;
}
return result;
}
}
2.请求线程
/**
* 网络Get请求的线程
*
* </BR> </BR> By:苦涩 </BR> 联系作者:QQ 534429149
* */
public class HttpGetThread implements Runnable {
private Handler hand;
private String url;
private MyGet myGet = new MyGet();
public HttpGetThread(Handler hand, String endParamerse) {
this.hand = hand;
// 拼接访问服务器完整的地址
url = Model.HTTPURL + endParamerse;
}
@Override
public void run() {
// 获取我们回调主ui的message
Message msg = hand.obtainMessage();
try {
String result = myGet.doGet(url);
msg.what = 200;
msg.obj = result;
} catch (ClientProtocolException e) {
msg.what = 404;
} catch (IOException e) {
msg.what = 100;
}
// 给主ui发送消息传递数据
hand.sendMessage(msg);
}
}
/**
* 网络Post请求的线程
* *
* </BR> </BR> By:苦涩 </BR> 联系作者:QQ 534429149
* */
public class HttpPostThread implements Runnable{
private Handler hand;
private String url;
private String mycode;
private String value;
private MyPost myGet = new MyPost();
public HttpPostThread(Handler hand,String endParamerse,String mycode,String value){
this.hand = hand;
//拼接访问服务器完整的地址
url = endParamerse;
this.mycode = mycode;
this.value = value;
}
@Override
public void run() {
//获取我们回调主ui的message
Message msg = hand.obtainMessage();
String result = myGet.doPost(url, mycode, value);
msg.what = 200;
msg.obj = result;
//给主ui发送消息传递数据
hand.sendMessage(msg);
}
}
/**
* 我们网络请求线程池:限制并行的网络请求线程。
*
* </BR> </BR> By:苦涩 </BR> 联系作者:QQ 534429149
* */
public class ThreadPoolUtils {
private ThreadPoolUtils() {
}
// 定义核心线程数,并行线程数
private static int CORE_POOL_SIZE = 3;
// 线程池最大线程数:除了正在运行的线程额外保存多少个线程
private static int MAX_POOL_SIZE = 200;
// 额外线程空闲状态生存时间
private static int KEEP_ALIVE_TIME = 5000;
// 阻塞队列。当核心线程队列满了放入的
// 初始化一个大小为10的泛型为Runnable的队列
private static BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(
10);
// 线程工厂,把传递进来的runnable对象生成一个Thread
private static ThreadFactory threadFactory = new ThreadFactory() {
// 原子型的integer变量生成的integer值不会重复
private final AtomicInteger ineger = new AtomicInteger();
@Override
public Thread newThread(Runnable arg0) {
return new Thread(arg0, "MyThreadPool thread:"
+ ineger.getAndIncrement());
}
};
// 当线程池发生异常的时候回调进入
private static RejectedExecutionHandler handler = new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// 进行重启操作
}
};
// 线程池ThreadPoolExecutor java自带的线程池
private static ThreadPoolExecutor threadpool;
// 静态代码块,在类被加载的时候进入
static {
threadpool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE,
KEEP_ALIVE_TIME, TimeUnit.SECONDS, workQueue, threadFactory,
handler);
}
public static void execute(Runnable runnable) {
threadpool.execute(runnable);
}
}
4.运用实例
1.get请求
ThreadPoolUtils.execute(new HttpGetThread(hand, url));
Handler hand = new Handler() {
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
if (msg.what == 404) {
Toast.makeText(ShopListActivity.this, "找不到地址", 1).show();
listBottemFlag = true;
} else if (msg.what == 100) {
Toast.makeText(ShopListActivity.this, "传输失败", 1).show();
listBottemFlag = true;
} else if (msg.what == 200) {
String result = (String) msg.obj;
// 在activity当中获取网络交互的数据
if (result != null) {
// 1次网络请求返回的数据
List<ShopInfo> newList = myJson.getShopList(result);
if (newList != null) {
if (newList.size() == 5) {
ListBottem.setVisibility(View.VISIBLE);
mStart += 5;
mEnd += 5;
} else {
ListBottem.setVisibility(View.GONE);
}
for (ShopInfo info : newList) {
list.add(info);
}
mAdapter.notifyDataSetChanged();
listBottemFlag = true;
mAdapter.notifyDataSetChanged();
}
}
mAdapter.notifyDataSetChanged();
}
};
};
2.post请求
String json = "{\"sid\":\"" + sid + "\"," + "\"pid\":\"" + pid + "\","
+ "\"signcontent\":\"" + signcontent + "\","
+ "\"signlevel\":\"" + signlevel + "\"," + "\"signimage\":\""
+ signimage + "\"," + "\"signtime\":\"" + signtime + "\"}";
ThreadPoolUtils.execute(new HttpPostThread(hand, Model.SIGNURL, "1",json));
Handler hand = new Handler() {
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
if (msg.what == 200) {
Toast.makeText(ShopDetailsCheckinActivity.this, "签到成功", 1)
.show();
finish();
}
}
};