一个是操作类,一个是junit test类,直接上代码:
org.apache.http.client.HttpClient.java
package org.example.httpclient;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
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.util.EntityUtils;
public class MHttpClient {
public void get() throws Exception {
// 创建HttpClient实例
HttpClient client = new DefaultHttpClient();
// 根据URL创建HttpGet实例
HttpGet get = new HttpGet("http://192.168.1.132:8088/WebServer/student.do");
// 执行get请求,得到返回体
HttpResponse response = client.execute(get);
// 判断是否正常返回
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 解析数据
String data = EntityUtils.toString(response.getEntity());
System.out.println(data);
}
}
public void post() throws Exception {
// 创建HttpClient实例
HttpClient client = new DefaultHttpClient();
// 根据URL创建HttpPost实例
HttpPost post = new HttpPost("http://192.168.1.132:8088/WebServer/student.do");
// 构造post参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", "11"));
// 编码格式转换
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);
// 传入请求体
post.setEntity(entity);
// 发送请求,得到响应体
HttpResponse response = client.execute(post);
// 判断是否正常返回
if (response.getStatusLine().getStatusCode() == 200) {
// 解析数据
HttpEntity resEntity = response.getEntity();
String data = EntityUtils.toString(resEntity);
System.out.println(data);
}
}
}
package org.example.httpclient;
import android.test.AndroidTestCase;
public class MHttpClientTestCast extends AndroidTestCase {
public void testGet() throws Exception {
MHttpClient client = new MHttpClient();
client.get();
}
public void testPost() throws Exception {
MHttpClient client = new MHttpClient();
client.post();
}
}