您的位置:首页 > 移动开发 > Android开发

Android客户端通过GET和POST向服务器发送数据

2013-05-15 21:52 661 查看
在进行开发之前,需要先了解HTTP协议及网络编程方面的知识。

Android

1.业务层类,通过使用android内置HttpClient发送GET、POST请求

/**
* 通过HttpClient发送Post请求
* @param path 请求路径
* @param params 请求参数
* @param ecoding 请求编码
* @return 请求是否成功
*/
private static boolean sendHttpClientPOSTRequest(String path,
Map<String, String> params, String ecoding) throws Exception {
List<NameValuePair> pair=new ArrayList<NameValuePair>();//存放请求参数
if(params!=null && !params.isEmpty()){
for (Map.Entry<String, String> entry : params.entrySet()) {
pair.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
}
UrlEncodedFormEntity enFormEntity=new UrlEncodedFormEntity(pair,ecoding);
HttpPost httpPost=new HttpPost(path);
httpPost.setEntity(enFormEntity);
DefaultHttpClient client=new DefaultHttpClient();
HttpResponse response=client.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
return true;
}
return false;
}

/**
* 通过HttpClient发送GET请求
* @param path 请求路径
* @param params 请求参数
* @param ecoding 请求编码
* @return 请求是否成功
*/
private static boolean sendHttpClientGETRequest(String path,
Map<String, String> params, String ecoding) throws Exception {
StringBuilder url=new StringBuilder(path);
url.append("?");
for (Map.Entry<String, String> entry : params.entrySet()) {
url.append(entry.getKey()).append("=");
url.append(URLEncoder.encode(entry.getValue(),ecoding));
url.append("&");
}
url.deleteCharAt(url.length()-1);
HttpGet httpGet=new HttpGet(url.toString());
DefaultHttpClient client=new DefaultHttpClient();
HttpResponse response=client.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
//接收返回的响应结果,也可以不接收
HttpEntity entity=response.getEntity();
EntityUtils.toString(entity,ecoding);//对响应结果进行编码
return true;
}
return false;
}
2.通过HTTP协议手动方式进行发送GET、POST请求

/**
* 发送POST请求
* @param path 请求路径
* @param params 请求参数
* @param ecoding 请求编码
* @return 请求是否成功
*/
private static boolean sendPOSTRequest(String path,
Map<String, String> params, String ecoding) throws Exception {
//title=kaka&timelength=10
//组拼实体数据
StringBuilder data=new StringBuilder();
if(params!=null && !params.isEmpty()){
for (Map.Entry<String, String> entry : params.entrySet()) {
data.append(entry.getKey()).append("=");
data.append(URLEncoder.encode(entry.getValue(),ecoding));
data.append("&");
}
data.deleteCharAt(data.length()-1);
}
byte[] entity=data.toString().getBytes();//得到实体数据
HttpURLConnection conn=(HttpURLConnection)new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);//设置允许对外输出数据
//设置头字段
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
OutputStream outStream=conn.getOutputStream();
outStream.write(entity);
if(conn.getResponseCode()==200){
return true;
}
return false;
}

/**
* 发送GET请求
* @param path 请求路径
* @param params 请求参数
* @param ecoding 请求编码
* @return 请求是否成功
*/
private static boolean sendGETRequest(String path,Map<String, String> params,String ecoding) throws Exception {
StringBuilder url=new StringBuilder(path);
url.append("?");
for (Map.Entry<String, String> entry : params.entrySet()) {
url.append(entry.getKey()).append("=");
url.append(URLEncoder.encode(entry.getValue(),ecoding));
url.append("&");
}
url.deleteCharAt(url.length()-1);
HttpURLConnection conn=(HttpURLConnection)new URL(url.toString()).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
return true;
}
return false;
}
3.调用示例

/**
* 调用示例保存数据
* @param title 标题
* @param length 时长
* @return
*/
public static boolean save(String title, String length) {
String path="http://192.168.1.111:8080/ManageServlet";//服务器路径
Map<String, String> params=new HashMap<String, String>();
params.put("title", title);
params.put("timelength", length);
try {
return sendHttpClientGETRequest(path,params,"UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
源码下载地址:Android客户端通过GET和POST向服务器发送数据源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐