标签:
Android中客户端请求服务器端的两种方式:Post方式和Get方式
在这里不直接赘述了,直接上源码如下:
(1).Post的方式:
/**
* Post的请求方式
*
* @param model
* 请求序号
* @param paramList
* 客户端请求的数据参数列表
* @return
*/
public JSONObject doPost(int model, List<NameValuePair> paramList) {
try {
// 客户端向服务器发送请求的数据
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "中国"));
params.add(new BasicNameValuePair("param2", "美国"));
// 1.定义请求的方式对象:
HttpPost post = new HttpPost(Base_URL);
// 2.定义请求的客户端对象
HttpClient client = new DefaultHttpClient();
// 将请求的数据封装在实体类中
HttpEntity entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
// 将实体类放置在URL中
post.setEntity(entity);
// 3.执行请求,返回响应的对象
HttpResponse response = client.execute(post);
if (response != null
&& response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 获得从服务器中返回的结果:从服务器中返回的结果是字符串的形式
String result = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);
if (result.startsWith("{")) {
try {
return new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
(2).Get方式:
/**
*
* @param model
* 请求序号
* @param paramList
* 客户端请求的数据参数列表
* @return
*/
public JSONObject doGet(int model, List<NameValuePair> paramList) {
// 客户端向服务器发送请求的数据
// List<BasicNameValuePair> params = new
// ArrayList<BasicNameValuePair>();
// params.add(new BasicNameValuePair("param1", "中国"));
// params.add(new BasicNameValuePair("param2", "美国"));
// 对参数编码
String param = URLEncodedUtils.format(paramList, HTTP.UTF_8);
// 将URL与参数拼接
// HttpGet get = new HttpGet(Base_URL + "?" + param);
HttpGet get = new HttpGet(Base_URL + "?" + param);
HttpClient httpClient = new DefaultHttpClient();
try {
// 发起GET请求
HttpResponse response = httpClient.execute(get);
if (response != null
&& response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 从服务器中获得响应的数据
String result = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);
try {
return new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
整个工具类代码如下:
package com.chengdong.su.downloaddemo.service;
import java.io.IOException;
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.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.client.utils.URLEncodedUtils;
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 org.json.JSONException;
import org.json.JSONObject;
/**
* 客户端向服务器端请求的工具
*
* @author scd
*
*/
public class HttpUtil {
/** 请求的URL */
private String Base_URL = "http://www.baidu.com";
/** TAG */
private String TAG = getClass().getSimpleName();
/**
* Post的请求方式
*
* @param model
* 请求序号
* @param paramList
* 客户端请求的数据参数列表
* @return
*/
public JSONObject doPost(int model, List<NameValuePair> paramList) {
try {
// 客户端向服务器发送请求的数据
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "中国"));
params.add(new BasicNameValuePair("param2", "美国"));
// 1.定义请求的方式对象:
HttpPost post = new HttpPost(Base_URL);
// 2.定义请求的客户端对象
HttpClient client = new DefaultHttpClient();
// 将请求的数据封装在实体类中
HttpEntity entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
// 将实体类放置在URL中
post.setEntity(entity);
// 3.执行请求,返回响应的对象
HttpResponse response = client.execute(post);
if (response != null
&& response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 获得从服务器中返回的结果:从服务器中返回的结果是字符串的形式
String result = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);
if (result.startsWith("{")) {
try {
return new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param model
* 请求序号
* @param paramList
* 客户端请求的数据参数列表
* @return
*/
public JSONObject doGet(int model, List<NameValuePair> paramList) {
// 客户端向服务器发送请求的数据
// List<BasicNameValuePair> params = new
// ArrayList<BasicNameValuePair>();
// params.add(new BasicNameValuePair("param1", "中国"));
// params.add(new BasicNameValuePair("param2", "美国"));
// 对参数编码
String param = URLEncodedUtils.format(paramList, HTTP.UTF_8);
// 将URL与参数拼接
// HttpGet get = new HttpGet(Base_URL + "?" + param);
HttpGet get = new HttpGet(Base_URL + "?" + param);
HttpClient httpClient = new DefaultHttpClient();
try {
// 发起GET请求
HttpResponse response = httpClient.execute(get);
if (response != null
&& response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 从服务器中获得响应的数据
String result = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);
try {
return new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u014225510/article/details/47112987