首先添加访问Internet权限:
<uses-permission android:name="android.permission.INTERNET"/>
示例代码:
public static boolean sendGetRequest(String path,
Map<String, String> params, String enc) throws Exception{
StringBuilder sb = new StringBuilder(path);
sb.append('?');
//?method=save&title=12345678&timelength=26&
//迭代Map拼接请求参数
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);//删除最后一个"&"
URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
if(conn.getResponseCode()==200){
return true;
}
return false;
}
public static boolean sendPostRequest(String path,
Map<String, String> params, String enc) throws Exception{
// title=dsfdsf&timelength=23&method=save
StringBuilder sb = new StringBuilder();
if(params!=null && !params.isEmpty()){
//迭代Map拼接请求参数
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);//删除最后一个"&"
}
byte[] entitydata = sb.toString().getBytes();//得到实体的二进制数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
//如果通过post提交数据,必须设置允许对外输出数据
conn.setDoOutput(true);
//此两参数必须设置
//Content-Type: application/x-www-form-urlencoded
//Content-Length: 38
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(entitydata);
outStream.flush();
outStream.close();
if(conn.getResponseCode()==200){
return true;
}
return false;
}
//HttpClient组件 SSL HTTPS Cookie
public static boolean sendRequestFromHttpClient(String path,
Map<String, String> params, String enc) throws Exception{
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
//得到经过编码过后的实体数据
UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);
HttpPost post = new HttpPost(path); //form
post.setEntity(entitydata);
DefaultHttpClient client = new DefaultHttpClient(); //浏览器
HttpResponse response = client.execute(post);//执行请求
if(response.getStatusLine().getStatusCode()==200){
return true;
}
return false;
}
单元测试:
public void testSendGetRequest() throws Throwable{
//?method=save&title=12345678&timelength=26
Map<String, String> params = new HashMap<String, String>();
params.put("method", "save");
params.put("title", "yaku");
params.put("timelength", "80");
HttpRequest.sendGetRequest("http://192.168.1.2:8080/webserver/server/manage.do", params, "UTF-8");
}
public void testSendPostRequest() throws Throwable{
Map<String, String> params = new HashMap<String, String>();
params.put("method", "save");
params.put("title", "胡汉三");
params.put("timelength", "80");
HttpRequest.sendPostRequest("http://192.168.1.2:8080/webserver/server/manage.do", params, "UTF-8");
}
public void testSendRequestFromHttpClient() throws Throwable{
Map<String, String> params = new HashMap<String, String>();
params.put("method", "save");
params.put("title", "开天");
params.put("timelength", "80");
HttpRequest.sendRequestFromHttpClient("http://192.168.1.2:8080/webserver/server/manage.do", params, "UTF-8");
}