• Android访问WCF(下篇)客户端开发


    本章目的: 实现在Android客户端请求我们上篇建立的WCF数据服务.

    此部分分为 建立Http请求 跟 接受WCF 返回的数据.

    一. 建立Http请求的方法

    1. protected String getRequest(String url, DefaultHttpClient client)  
    2.             throws Exception {  
    3.         String result = null;  
    4.         int statusCode = 0;  
    5.         HttpGet getMethod = new HttpGet(url);  
    6.         Log.d(TAG, "do the getRequest,url=" + url + "");  
    7.         try {  
    8.             getMethod.setHeader("User-Agent", USER_AGENT);  
    9.             // HttpParams params = new HttpParams();  
    10.   
    11.             // 添加用户密码验证信息  
    12.             // client.getCredentialsProvider().setCredentials(  
    13.             // new AuthScope(null, -1),  
    14.             // new UsernamePasswordCredentials(mUsername, mPassword));  
    15.   
    16.             HttpResponse httpResponse = client.execute(getMethod);  
    17.             // statusCode == 200 正常  
    18.             statusCode = httpResponse.getStatusLine().getStatusCode();  
    19.             Log.d(TAG, "statuscode = " + statusCode);  
    20.             // 处理返回的httpResponse信息  
    21.             result = retrieveInputStream(httpResponse.getEntity());  
    22.         } catch (Exception e) {  
    23.             Log.e(TAG, e.getMessage());  
    24.             throw new Exception(e);  
    25.         } finally {  
    26.             getMethod.abort();  
    27.         }  
    28.         return result;  
    29.     }  


     

    参数URL: 我们要请求的地址

    Client:  这个可以直接用new DefaultHttpClient(new BasicHttpParams()) 来初始化.

    这个方法中需要注意RetrieveInputStream方法, 这个是当Http请求完成之后, 用来处理服务器返回数据的方法,

    二. 接受从WCF端传回的数据

    1. protected  String retrieveInputStream(HttpEntity httpEntity) {  
    2.         int  length = (int) httpEntity.getContentLength();  
    3.         if  (length < 0)  
    4.             length = 10000;  
    5.         StringBuffer stringBuffer = new  StringBuffer(length);  
    6.         try  {  
    7.             InputStreamReader inputStreamReader = new  InputStreamReader(  
    8.                     httpEntity.getContent(), HTTP.UTF_8);  
    9.             char  buffer[] = new char[length];  
    10.             int  count;  
    11.             while  ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {  
    12.                 stringBuffer.append(buffer, 0, count);  
    13.             }  
    14.         } catch  (UnsupportedEncodingException e) {  
    15.             Log.e(TAG, e.getMessage());  
    16.         } catch  (IllegalStateException e) {  
    17.             Log.e(TAG, e.getMessage());  
    18.         } catch  (IOException e) {  
    19.             Log.e(TAG, e.getMessage());  
    20.         }  
    21.         return  stringBuffer.toString();  
    22.     }  


     

    此方法在接受到WCF服务端返回的数据之后,  转换程String类型返回.

    附加内容:

    1. private static final String BASE_URL = "http://10.0.2.2:82/BlogCategoryService/";  
    2. private static final String EXTENSION = "Json/";;  
    3. private static final String TAG = "API";  
    4. private static final String USER_AGENT = "Mozilla/4.5";  
    5.   
    6. public JSONObject getObject(String sbj) throws JSONException, Exception {  
    7.     return new JSONObject(getRequest(BASE_URL + EXTENSION + sbj));  
    8. }  
    9.   
    10. public JSONArray getArray(String sbj) throws JSONException,  
    11.         Exception {  
    12.     return new JSONArray(getRequest(BASE_URL + EXTENSION + sbj));  
    13. }  
    14.   
    15. protected String getRequest(String url) throws Exception {  
    16.     return getRequest(url, new DefaultHttpClient(new BasicHttpParams()));  
    17. }  


     

    请求数据之前封装方法:

    总结 : 此篇主要说明了Http请求的的两个阶段, 建立请求跟接受服务器返回的数据, 在下篇再主要说明如何处理服务端返回的JSON数据,并把数据显示在UI上面.

  • 相关阅读:
    react开发环境准备
    react介绍
    课程大纲
    Proving Equivalences HDU-2767 (tarjan缩点)
    tarjan求强连通分量 + 缩点 + 求割点割边
    树的重心(性质+模版)
    Educational Codeforces Round 93 (Rated for Div. 2)(A B C D)
    Friend-Graph HDU
    Codeforces Round #665 (Div. 2) (A B C D E)
    Matrix Again HDU
  • 原文地址:https://www.cnblogs.com/afly/p/2398179.html
Copyright © 2020-2023  润新知