• 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.         }  
  • 相关阅读:
    淘宝网六个质量属性场景
    架构漫谈阅读笔记2
    poj 3304
    poj 2318
    bzoj 4008
    任意模数NTT
    CF623E
    CF712E
    bzoj 1925
    bzoj 4710
  • 原文地址:https://www.cnblogs.com/wanghuaijun/p/5465141.html
Copyright © 2020-2023  润新知