Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过Android单元测试来完成这四种方法的,还不清楚Android的单元测试的同学们请看Android开发技巧总结中的Android单元测试的步骤一文。

java.net包中的HttpURLConnection类

Get方式:

view plain copy to clipboard print ?
  1. //Get方式请求
  2. publicstaticvoidrequestByGet()throwsException{
  3. Stringpath="https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
  4. //新建一个URL对象
  5. URLurl=newURL(path);
  6. //打开一个HttpURLConnection连接
  7. HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();
  8. //设置连接超时时间
  9. urlConn.setConnectTimeout(5*1000);
  10. //开始连接
  11. urlConn.connect();
  12. //判断请求是否成功
  13. if(urlConn.getResponseCode()==HTTP_200){
  14. //获取返回的数据
  15. byte[]data=readStream(urlConn.getInputStream());
  16. Log.i(TAG_GET,"Get方式请求成功,返回数据如下:");
  17. Log.i(TAG_GET,newString(data,"UTF-8"));
  18. }else{
  19. Log.i(TAG_GET,"Get方式请求失败");
  20. }
  21. //关闭连接
  22. urlConn.disconnect();
  23. }

Post方式:

view plain copy to clipboard print ?
  1. //Post方式请求
  2. publicstaticvoidrequestByPost()throwsThrowable{
  3. Stringpath="https://reg.163.com/logins.jsp";
  4. //请求的参数转换为byte数组
  5. Stringparams="id="+URLEncoder.encode("helloworld","UTF-8")
  6. +"&pwd="+URLEncoder.encode("android","UTF-8");
  7. byte[]postData=params.getBytes();
  8. //新建一个URL对象
  9. URLurl=newURL(path);
  10. //打开一个HttpURLConnection连接
  11. HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();
  12. //设置连接超时时间
  13. urlConn.setConnectTimeout(5*1000);
  14. //Post请求必须设置允许输出
  15. urlConn.setDoOutput(true);
  16. //Post请求不能使用缓存
  17. urlConn.setUseCaches(false);
  18. //设置为Post请求
  19. urlConn.setRequestMethod("POST");
  20. urlConn.setInstanceFollowRedirects(true);
  21. //配置请求Content-Type
  22. urlConn.setRequestProperty("Content-Type",
  23. "application/x-www-form-urlencode");
  24. //开始连接
  25. urlConn.connect();
  26. //发送请求参数
  27. DataOutputStreamdos=newDataOutputStream(urlConn.getOutputStream());
  28. dos.write(postData);
  29. dos.flush();
  30. dos.close();
  31. //判断请求是否成功
  32. if(urlConn.getResponseCode()==HTTP_200){
  33. //获取返回的数据
  34. byte[]data=readStream(urlConn.getInputStream());
  35. Log.i(TAG_POST,"Post请求方式成功,返回数据如下:");
  36. Log.i(TAG_POST,newString(data,"UTF-8"));
  37. }else{
  38. Log.i(TAG_POST,"Post方式请求失败");
  39. }
  40. }

org.apache.http包中的HttpGet和HttpPost类

Get方式:

view plain copy to clipboard print ?
  1. //HttpGet方式请求
  2. publicstaticvoidrequestByHttpGet()throwsException{
  3. Stringpath="https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
  4. //新建HttpGet对象
  5. HttpGethttpGet=newHttpGet(path);
  6. //获取HttpClient对象
  7. HttpClienthttpClient=newDefaultHttpClient();
  8. //获取HttpResponse实例
  9. HttpResponsehttpResp=httpClient.execute(httpGet);
  10. //判断是够请求成功
  11. if(httpResp.getStatusLine().getStatusCode()==HTTP_200){
  12. //获取返回的数据
  13. Stringresult=EntityUtils.toString(httpResp.getEntity(),"UTF-8");
  14. Log.i(TAG_HTTPGET,"HttpGet方式请求成功,返回数据如下:");
  15. Log.i(TAG_HTTPGET,result);
  16. }else{
  17. Log.i(TAG_HTTPGET,"HttpGet方式请求失败");
  18. }
  19. }

Post方式:

view plain copy to clipboard print ?
  1. //HttpPost方式请求
  2. publicstaticvoidrequestByHttpPost()throwsException{
  3. Stringpath="https://reg.163.com/logins.jsp";
  4. //新建HttpPost对象
  5. HttpPosthttpPost=newHttpPost(path);
  6. //Post参数
  7. List<NameValuePair>params=newArrayList<NameValuePair>();
  8. params.add(newBasicNameValuePair("id","helloworld"));
  9. params.add(newBasicNameValuePair("pwd","android"));
  10. //设置字符集
  11. HttpEntityentity=newUrlEncodedFormEntity(params,HTTP.UTF_8);
  12. //设置参数实体
  13. httpPost.setEntity(entity);
  14. //获取HttpClient对象
  15. HttpClienthttpClient=newDefaultHttpClient();
  16. //获取HttpResponse实例
  17. HttpResponsehttpResp=httpClient.execute(httpPost);
  18. //判断是够请求成功
  19. if(httpResp.getStatusLine().getStatusCode()==HTTP_200){
  20. //获取返回的数据
  21. Stringresult=EntityUtils.toString(httpResp.getEntity(),"UTF-8");
  22. Log.i(TAG_HTTPGET,"HttpPost方式请求成功,返回数据如下:");
  23. Log.i(TAG_HTTPGET,result);
  24. }else{
  25. Log.i(TAG_HTTPGET,"HttpPost方式请求失败");
  26. }
  27. }

更多相关文章

  1. Android中如何自己创造一个Cursor及MatrixCursor源码分析
  2. Android黑科技动态加载(三)之动态加载资源
  3. [ ]Android(安卓)Post请求 RestFull Wcf
  4. ContentProvider
  5. Android通过WebView与JS交互的全面方式
  6. 在 Android(安卓)上使用协程(三) :Real Work
  7. delphi XE开发微信支付Android获取手机存储权限、Android获取短
  8. Android(安卓)各大网络请求库的比较及实战
  9. Android(安卓)下使用 JSON 实现 HTTP 请求

随机推荐

  1. 常用的Android(安卓)Widget组件学习②-Ed
  2. Android中ListView添加事件并获取选中项
  3. FrameLayout的使用——android开发之xml
  4. android 胡言乱语 2 android UI
  5. 如何使用Android MediaStore裁剪大图片
  6. Android Permission 大全
  7. android 1.6 launcher研究之Drag&Drop模
  8. Dev Guide/Framework Topics/Search-版本
  9. 【Android】进程通信IPC——Messenger
  10. Android UI--listview更改选中时item背景