Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过Android单元测试来完成这四种方法的,还不清楚Android的单元测试的同学们请看 Android开发技巧总结 中的Android单元测试的步骤一文。
java.net包中的HttpURLConnection类
Get方式:
02 | public static void requestByGet() throws Exception { |
03 | String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android" ; |
05 | URL url = new URL(path); |
06 | // 打开一个HttpURLConnection连接 |
07 | HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); |
09 | urlConn.setConnectTimeout( 5 * 1000 ); |
13 | if (urlConn.getResponseCode() == HTTP_200) { |
15 | byte [] data = readStream(urlConn.getInputStream()); |
16 | Log.i(TAG_GET, "Get方式请求成功,返回数据如下:" ); |
17 | Log.i(TAG_GET, new String(data, "UTF-8" )); |
19 | Log.i(TAG_GET, "Get方式请求失败" ); |
Post方式:
02 | public static void requestByPost() throws Throwable { |
03 | String path = "https://reg.163.com/logins.jsp" ; |
05 | String params = "id=" + URLEncoder.encode( "helloworld" , "UTF-8" ) |
06 | + "&pwd=" + URLEncoder.encode( "android" , "UTF-8" ); |
07 | byte [] postData = params.getBytes(); |
09 | URL url = new URL(path); |
10 | // 打开一个HttpURLConnection连接 |
11 | HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); |
13 | urlConn.setConnectTimeout( 5 * 1000 ); |
15 | urlConn.setDoOutput( true ); |
17 | urlConn.setUseCaches( false ); |
19 | urlConn.setRequestMethod( "POST" ); |
20 | urlConn.setInstanceFollowRedirects( true ); |
22 | urlConn.setRequestProperty( "Content-Type" , |
23 | "application/x-www-form-urlencode" ); |
27 | DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream()); |
32 | if (urlConn.getResponseCode() == HTTP_200) { |
34 | byte [] data = readStream(urlConn.getInputStream()); |
35 | Log.i(TAG_POST, "Post请求方式成功,返回数据如下:" ); |
36 | Log.i(TAG_POST, new String(data, "UTF-8" )); |
38 | Log.i(TAG_POST, "Post方式请求失败" ); |
org.apache.http包中的HttpGet和HttpPost类
Get方式:
02 | public static void requestByHttpGet() throws Exception { |
03 | String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android" ; |
05 | HttpGet httpGet = new HttpGet(path); |
07 | HttpClient httpClient = new DefaultHttpClient(); |
09 | HttpResponse httpResp = httpClient.execute(httpGet); |
11 | if (httpResp.getStatusLine().getStatusCode() == HTTP_200) { |
13 | String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8" ); |
14 | Log.i(TAG_HTTPGET, "HttpGet方式请求成功,返回数据如下:" ); |
15 | Log.i(TAG_HTTPGET, result); |
17 | Log.i(TAG_HTTPGET, "HttpGet方式请求失败" ); |
Post方式:
02 | public static void requestByHttpPost() throws Exception { |
03 | String path = "https://reg.163.com/logins.jsp" ; |
05 | HttpPost httpPost = new HttpPost(path); |
07 | List params = new ArrayList(); |
08 | params.add( new BasicNameValuePair( "id" , "helloworld" )); |
09 | params.add( new BasicNameValuePair( "pwd" , "android" )); |
11 | HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); |
13 | httpPost.setEntity(entity); |
15 | HttpClient httpClient = new DefaultHttpClient(); |
17 | HttpResponse httpResp = httpClient.execute(httpPost); |
19 | if (httpResp.getStatusLine().getStatusCode() == HTTP_200) { |
21 | String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8" ); |
22 | Log.i(TAG_HTTPGET, "HttpPost方式请求成功,返回数据如下:" ); |
23 | Log.i(TAG_HTTPGET, result); |
25 | Log.i(TAG_HTTPGET, "HttpPost方式请求失败" ); |
以上是一些部分代码,测试的时候在测试类中运行对应的测试方法即可