Android中的几种网络请求方式详解
分类:手机开发| 发布:camnprbubuol| 查看: | 发表时间:2011/12/15
Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过Android单元测试来完成这四种方法的,还不清楚Android的单元测试的同学们请看Android开发技巧总结中的Android单元测试的步骤一文。
java.net包中的HttpURLConnection类
Get方式:
-
- public static void requestByGet() throws Exception {
- String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
-
- URL url = new URL(path);
-
- HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
-
- urlConn.setConnectTimeout(5 * 1000);
-
- urlConn.connect();
-
- if (urlConn.getResponseCode() == HTTP_200) {
-
- byte[] data = readStream(urlConn.getInputStream());
- Log.i(TAG_GET, "Get方式请求成功,返回数据如下:");
- Log.i(TAG_GET, new String(data, "UTF-8"));
- } else {
- Log.i(TAG_GET, "Get方式请求失败");
- }
-
- urlConn.disconnect();
- }
Post方式:
-
- public static void requestByPost() throws Throwable {
- String path = "https://reg.163.com/logins.jsp";
-
- String params = "id=" + URLEncoder.encode("helloworld", "UTF-8")
- + "&pwd=" + URLEncoder.encode("android", "UTF-8");
- byte[] postData = params.getBytes();
-
- URL url = new URL(path);
-
- HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
-
- urlConn.setConnectTimeout(5 * 1000);
-
- urlConn.setDoOutput(true);
-
- urlConn.setUseCaches(false);
-
- urlConn.setRequestMethod("POST");
- urlConn.setInstanceFollowRedirects(true);
-
- urlConn.setRequestProperty("Content-Type",
- "application/x-www-form-urlencode");
-
- urlConn.connect();
-
- DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
- dos.write(postData);
- dos.flush();
- dos.close();
-
- if (urlConn.getResponseCode() == HTTP_200) {
-
- byte[] data = readStream(urlConn.getInputStream());
- Log.i(TAG_POST, "Post请求方式成功,返回数据如下:");
- Log.i(TAG_POST, new String(data, "UTF-8"));
- } else {
- Log.i(TAG_POST, "Post方式请求失败");
- }
- }
org.apache.http包中的HttpGet和HttpPost类
Get方式:
-
- public static void requestByHttpGet() throws Exception {
- String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
-
- HttpGet httpGet = new HttpGet(path);
-
- HttpClient httpClient = new DefaultHttpClient();
-
- HttpResponse httpResp = httpClient.execute(httpGet);
-
- if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
-
- String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
- Log.i(TAG_HTTPGET, "HttpGet方式请求成功,返回数据如下:");
- Log.i(TAG_HTTPGET, result);
- } else {
- Log.i(TAG_HTTPGET, "HttpGet方式请求失败");
- }
- }
Post方式:
-
- public static void requestByHttpPost() throws Exception {
- String path = "https://reg.163.com/logins.jsp";
-
- HttpPost httpPost = new HttpPost(path);
-
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new BasicNameValuePair("id", "helloworld"));
- params.add(new BasicNameValuePair("pwd", "android"));
-
- HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
-
- httpPost.setEntity(entity);
-
- HttpClient httpClient = new DefaultHttpClient();
-
- HttpResponse httpResp = httpClient.execute(httpPost);
-
- if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
-
- String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
- Log.i(TAG_HTTPGET, "HttpPost方式请求成功,返回数据如下:");
- Log.i(TAG_HTTPGET, result);
- } else {
- Log.i(TAG_HTTPGET, "HttpPost方式请求失败");
- }
- }
以上是一些部分代码,测试的时候在测试类中运行对应的测试方法即可。完整代码点这里下载
来源:http://blog.csdn.net/zuolongsnail/article/details/6373051