最近公司要我调试android接口,就封装了一下,记录下来。。做为笔记吧!
package com.dw.test;
public class User {
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + "]";
}
}
package com.dw.util;
import java.util.List;
import net.sf.json.JSONArray;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil<T> {
/**
* post请求方法, 返回一个对象集合
*
* @param url 请求的url
* @param objectClass class对象,你想要返回的对象
* @param formparams 请求参数
* @return List<T> 返回对象集合
* @throws Exception
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<T> getListPost(String url, Class objectClass,
List<NameValuePair> formparams) throws Exception {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
List<T> objList = null;
try {
// 创建httppost
HttpPost httppost = new HttpPost(url);
// 设置请求的表单编码
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(
formparams, "UTF-8");
// 设置http请求参数
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getRequestLine());
// 发送请求
CloseableHttpResponse response = httpclient.execute(httppost);
// 请求响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
try {
// 获取响应的数据
HttpEntity entity = response.getEntity();
if (entity != null) {
String json = EntityUtils.toString(entity, "UTF-8");
System.out.println("Response content: " + json);
JSONArray jsonobj = JSONArray.fromObject(json);
objList = (List<T>) JSONArray.toCollection(jsonobj,
objectClass);
}
} finally {
response.close();
}
} else {
System.out.println("请求失败!");
}
} finally {
httpclient.close();
}
return objList;
}
/**
* post请求方法, 返回一个对象
*
* @param url 请求的url
* @param objectClass class对象,你想要返回的对象
* @param formparams 设置请求参数
* @return T 返回对象
* @throws Exception
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public T getObjPost(String url, Class objectClass,
List<NameValuePair> formparams) throws Exception {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
T obj = null;
try {
// 创建httppost
HttpPost httppost = new HttpPost(url);
// 设置请求的表单编码
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(
formparams, "UTF-8");
// 设置http请求参数
httppost.setEntity(uefEntity);
System.out
.println("executing request " + httppost.getRequestLine());
// 发送请求
CloseableHttpResponse response = httpclient.execute(httppost);
// 请求响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
try {
// 获取响应的数据
HttpEntity entity = response.getEntity();
if (entity != null) {
String json = EntityUtils.toString(entity, "UTF-8");
System.out.println("Response content: " + json);
JSONArray jsonobj = JSONArray.fromObject(json);
List<T> objList = (List<T>) JSONArray.toCollection(
jsonobj, objectClass);
if (objList != null && objList.size() > 0) {
if (objList.size() <= 1) {
obj = objList.get(0);
}
}
}
} finally {
response.close();
}
} else {
System.out.println("请求失败!");
}
} finally {
httpclient.close();
}
return obj;
}
}
package com.dw.util;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.dw.test.User;
public class HttpClientTest {
private static HttpClientUtil<User> http = null;
private static List<NameValuePair> formparams = null;
@Before
public void newUser() {
http = new HttpClientUtil<User>();
formparams = new ArrayList<NameValuePair>();
}
@After
public void clerformparams() {
formparams = null;
}
@Test
public void postListTest() throws Exception {
//请求参数
formparams.add(new BasicNameValuePair("names", "邓文伟"));
formparams.add(new BasicNameValuePair("password", "123"));
List<User> listUser = http.getListPost(
"http://localhost:8080/testPlatform/indexs.htm", User.class,
formparams);
System.out.println(listUser);
}
@Test
public void postUserTest() throws Exception {
//请求参数
formparams.add(new BasicNameValuePair("names", "邓文伟"));
formparams.add(new BasicNameValuePair("password", "123"));
User user= http.getObjPost(
"http://localhost:8080/testPlatform/indexs.htm", User.class,
formparams);
System.out.println(user.getUsername());
}
}
代码和jar包:http://download.csdn.net/detail/dengwenwei121/8168261
分享到:
相关推荐
最近调试android接口,封装了几个 HttpClient通用方法
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
这是我一个抠图程序发送请求时专用的jar,maven仓库不好下载,我就上传到csdn,以便下次可以继续使用
httpcore-4.2.4,httpclient-4.2.5,httpclient-cache-4.2.5,httpmime-4.2.5的jar包下载。 比如微信退款需要以上jar包
httpclient-android-4.3.5.1.jar,用于解决Android使用高版本httpclient出错的问题,下载完直接放到libs文件夹下,然后右键-build path-add to build path,然后在buildpath configuration 里面勾上,就搞定了
httpclient-android Build script and dependencies to create repackaged upstream version of HttpClient and depdendencies (HttpMime, HttpCore, HttpClient-Cache) and get it working on Android API version...
赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...
赠送jar包:httpclient-4.4.1.jar; 赠送原API文档:httpclient-4.4.1-javadoc.jar; 赠送源代码:httpclient-4.4.1-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.1.pom; 包含翻译后的API文档:httpclient...
wechatpay-apache-httpclient-0.2.1.jar
赠送jar包:httpclient-4.5.3.jar; 赠送原API文档:httpclient-4.5.3-javadoc.jar; 赠送源代码:httpclient-4.5.3-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.3.pom; 包含翻译后的API文档:httpclient...
httpclient-tutorial-simplified-chinese.pdf
赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...
commons-httpclient.zip
赠送jar包:httpclient-4.5.3.jar 赠送原API文档:httpclient-4.5.3-javadoc.jar 赠送源代码:httpclient-4.5.3-sources.jar 包含翻译后的API文档:httpclient-4.5.3-javadoc-API文档-中文(简体)版.zip 对应Maven...
赠送jar包:httpclient-4.5.12.jar; 赠送原API文档:httpclient-4.5.12-javadoc.jar; 赠送源代码:httpclient-4.5.12-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.12.pom; 包含翻译后的API文档:...
httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar
commons-httpclient包和ksoap2-android-assembly包是android调用webservices需要加载的包。朋友们可以下载。
赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip ...
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
HttpClient for android_4.3.5.jar 编译好的jar文件,含有两种,一种是srcjar,一种是jar