package com.sxt.jcjd.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; import android.os.DropBoxManager.Entry;
import android.os.Handler;
import android.os.Message; public class HttpUtil { private HashMap<String, String> map;
private String strUrl;
private String strMethod;
private HttpGet httpGet;
private HttpClient client;
private HttpPost post;
private Handler handler;
public HttpUtil(HashMap<String, String> map,String strUrl, Handler handler)
{
this.map = map;
this.strUrl = strUrl;
this.handler = handler;
} public void ExecuteGetRequest()
{ Thread thread = new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
try {
String result = "";
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext())
{
Map.Entry entry = (Map.Entry)iterator.next();
String key = entry.getKey().toString();
String value = entry.getValue().toString();
strUrl += key+"="+value+"&";
}
strUrl = strUrl.substring(0, strUrl.length() - 1);
httpGet = new HttpGet(strUrl);
client = new DefaultHttpClient();
HttpResponse response = client.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
result = EntityUtils.toString(response.getEntity());
}
Message msg = new Message();
msg.obj = result;
handler.sendMessage(msg);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
thread.start();
} public void ExecutePostRequest()
{
Thread thread = new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
String result = "";
List<NameValuePair> list = new ArrayList<NameValuePair>();
try {
post = new HttpPost(strUrl);
client = new DefaultHttpClient();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext())
{
Map.Entry entry = (Map.Entry)iterator.next();
String key = entry.getKey().toString();
String value = entry.getValue().toString();
NameValuePair nv = new BasicNameValuePair(key, value);
list.add(nv);
}
post.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode() == 200)
{
result = EntityUtils.toString(response.getEntity());
}
Message msg = new Message();
msg.obj = result;
handler.sendMessage(msg);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
thread.start(); }
}

自己写的Android端HttpUtil工具类的更多相关文章

  1. 一个使用命令行编译Android项目的工具类

    一个使用命令行编译Android项目的工具类 简单介绍 编译apk项目须要使用的几个工具,基本都在sdk中,它们各自是(Windows系统): 1.aapt.exe 资源打包工具 2.android. ...

  2. HttpUtil工具类

    HttpUtil工具类 /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param params * 请求参数,请求参数应该是name1=val ...

  3. Android 软件管理工具类Utils

    Android 软件管理工具类Utils /** * Created by uilubo on 2015/9/30. * 工具类 */ public class Utils { public stat ...

  4. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

  5. HttpUtil工具类,发送Get/Post请求,支持Http和Https协议

    HttpUtil工具类,发送Get/Post请求,支持Http和Https协议 使用用Httpclient封装的HttpUtil工具类,发送Get/Post请求 1. maven引入httpclien ...

  6. Android开发常用工具类

    来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...

  7. Android常用的工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Prefe ...

  8. Android常用的工具类(转)

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...

  9. Android颜色转换工具类ColorUtil

    项目中需要根据ScrollView的滚动距离来动态设置Topbar的背景透明度,网上有类似的开源库FadingActionBar,使用的是ActionBar做的.而我的项目中并没有使用ActionBa ...

随机推荐

  1. linux 作为web应用服务器内核参数/etc/sysctl.conf

    # Kernel sysctl configuration file for Red Hat Linux## For binary values, 0 is disabled, 1 is enable ...

  2. POJ 1905 Expanding Rods (求直杆弯曲拱起的高度)(二分法,相交弦定理)

    Description When a thin rod of length L is heated n degrees, it expands to a new length L' = (1+n*C) ...

  3. 【UVA 11181】(条件概率)

    题链:https://cn.vjudge.net/problem/UVA-11181 题意 n个人去了超市,已知每个人买东西的概率为p[i],在已知有r个人买了东西的情况下,求实际上每个人买东西的概率 ...

  4. PAT 1010. 一元多项式求导

    1010. 一元多项式求导 (25) 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数 ...

  5. resin web项目的 编码问题

    问题描述: 服务器迁移,迁移以后Linux系统编码由 UTF-8 变成了GBK !!! 导致在resin 中运行java web项目,调用 http 接口,解析http 接口的返回内容 如:xml 时 ...

  6. Gearman 初窥【转载】

    Gearman是一个分发任务的程序框架,可以用在各种场合,与Hadoop相 比,Gearman更偏向于任务分发功能.它的任务分布非常简单,简单得可以只需要用脚本即可完成.Gearman最初用于Live ...

  7. CSY版最大团,速度快一倍

    #include <bits/stdc++.h> using namespace std; #define REP(i, n) for(int i(0); i < (n); ++i) ...

  8. struts2常用类型的Result

    2.2.1. dispatcher dispatcher类型是用于转发的Result,可以将请求转发给JSP.这种类型的Result对应的类为 ServletDispatcherResult,它是St ...

  9. JDBC调用存储过程,进参出参

    今天做了一个数据表拷贝的功能,用到了存储过程,就写了一个java中用jdbc调用存储过程的代码,成功的实现了功能,晚上跑回家记录下 Connection conn = ConnectionUtil.g ...

  10. Hibernate学习笔记(四)

    我是从b站视频上学习的hibernate框架,其中有很多和当前版本不符合之处,我在笔记中进行了修改以下是b站视频地址:https://www.bilibili.com/video/av14626440 ...