Android 通过http访问服务器

目前Android 与服务器交互有两种方式:1.Socket 2. Http ;

但由于Http的封装性以及性能比socket要好,所以推荐使用http方式和服务器交互;

通过http访问服务器有三种方法:1.post  2. get  3.或者上传文件

例子如下:

[java]  view plain copy
  1. protected JSONObject toWebService(String url, String method,  
  2.                 List<NameValuePair> params) {  
  3.             try {  
  4.                 Log.w("HealthCareAPI""toWebService: url = " + url);  
  5.                 HttpUriRequest request = null;  
  6.                 if (HealthCareApi.GET_METHOD.equalsIgnoreCase(method)) {  //Get方法  
  7.                     url = url + "?" + URLEncodedUtils.format(params, "UTF-8");  
  8.                     Log.w("HealthCareApiCore", url);  
  9.                     request = new HttpGet(url);  
  10.                 } else if (HealthCareApi.POST_METHOD.equalsIgnoreCase(method)) {  //Post方法  
  11.                     HttpPost postRequest = new HttpPost(url);  
  12.                     UrlEncodedFormEntity entity = new UrlEncodedFormEntity(  
  13.                             params, HTTP.UTF_8);  
  14.                     postRequest.setHeader("Content-Type",  
  15.                             "application/x-www-form-urlencoded; charset=utf-8");  
  16.                     postRequest.setEntity(entity);  
  17.                     request = postRequest;  
  18.                 } else if (HealthCareApi.POST_METHOD_WITH_BIN      
  19.                         .equalsIgnoreCase(method)) {   //上传文件,这里指的是上传图片文件  
  20.                     HttpPost postRequest = new HttpPost(url);  
  21.                     MultipartEntity multipartContent = new MultipartEntity();  
  22.                     for (NameValuePair nameValuePair : params) {  
  23.                         if (nameValuePair.getName().equals("photo")  
  24.                                 || nameValuePair.getName().equals("pic")  
  25.                                 || nameValuePair.getName().equals("photo_url")) {  
  26.                             File photoFile = new File(nameValuePair.getValue());  
  27.                             if (photoFile.exists()) {  
  28.                                 String extensionName = HealthCareApi  
  29.                                         .getExtensionName(photoFile.getName());  
  30.                                 FileBody fileBody = new FileBody(photoFile,  
  31.                                         MimeTypeMap.getSingleton()  
  32.                                                 .getMimeTypeFromExtension(  
  33.                                                         new String(extensionName).toLowerCase()));  
  34.                                 multipartContent.addPart(  
  35.                                         nameValuePair.getName(), fileBody);  
  36.                             }  
  37.                         } else {  
  38.                             multipartContent.addPart(nameValuePair.getName(),  
  39.                                     new StringBody(nameValuePair.getValue(),  
  40.                                             Charset.forName("UTF-8")));  
  41.                         }  
  42.                     }  
  43.                     postRequest.setEntity(multipartContent);  
  44.                     request = postRequest;  
  45.                 }  
  46.                 HttpParams httpParams = new BasicHttpParams();  
  47.                 HttpConnectionParams  
  48.                         .setConnectionTimeout(httpParams, 20 * 1000);  
  49.                 HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);  
  50.                 HttpResponse httpResponse = new DefaultHttpClient(httpParams)  
  51.                         .execute(request);  
  52.                 HttpEntity entity = httpResponse.getEntity();  
  53.                 String res = EntityUtils.toString(entity, "UTF-8");  
  54.                 Log.w("HealtCareApi""res: " + res);  
  55.                 JSONObject object = new JSONObject(res);  
  56.                 return object;  
  57.             } catch (Exception e) {  
  58.                 e.printStackTrace();  
  59.                 mException = e;  
  60.             }  
  61.             return null;  
  62.         }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值