2016-08-22 28 views
0

您好我正在使用舊的Android項目,其中使用org.apache.httpclient類現在折舊,將從API 22中刪除。我們如何進行遷移到httpUrlConnection,但仍保持原有功能?我所有的HTTP POST現在不與錯誤錯誤發送和接收XML和JSON Android

22 16:16:33.755 5553-6064/com.singPost D/SingPost: payload: <OverseasPostalInfoDetailsRequest xmlns="http://singpost.com/paw/ns"><Country>AFAFG</Country><Weight>100</Weight><DeliveryServiceName></DeliveryServiceName><ItemType></ItemType><PriceRange>999</PriceRange><DeliveryTimeRange>999</DeliveryTimeRange></OverseasPostalInfoDetailsRequest> 
08-22 16:16:33.755 5553-6064/com.singPost D/SingPost: Utils-request: https://prdesb1.singpost.com/ma/FilterOverseasPostalInfo 
08-22 16:16:35.667 5553-6064/com.singPost E/SingPost: IOException: javax.net.ssl.SSLHandshakeException: Handshake failed 
08-22 16:16:35.691 5553-6008/com.singPost E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f67d844e5c0 

在這裏工作是我的web API調用:

public static String callWS(String url, String module, String method, String payload) throws UnsupportedEncodingException, ClientProtocolException, IOException, ParseException, Exception{ 
     HttpClient httpsClient; 
     HttpPost httppost; 

     HttpResponse response = null; 
     String ret = ""; 

     httpsClient = getHTTPSClient(); 

     String request = url; 
     if(!module.equalsIgnoreCase("") && !method.equalsIgnoreCase("")){ 
      request += "/" + module + "/" + method; 
     }else if(!module.equalsIgnoreCase("")){ 
      request += "/" + module; 
     }else if(!method.equalsIgnoreCase("")){ 
      request += "/" + method; 
     } 

     if(Constants.DEBUG_MODE){ 
      Log.d(Constants.TAG, Utils.class.getSimpleName() + "-request: " + request); 
     } 

     try { 

     httppost = new HttpPost(request); 
     httppost.setHeader("Content-Type", Constants.contentType); 
     if(payload!=null && !payload.equalsIgnoreCase("")){ 
      httppost.setEntity(new StringEntity(payload, HTTP.UTF_8)); 
     } 
     response = httpsClient.execute(httppost); 
     System.out.println("??**"+response.toString()); 

     if (response != null) { 
      ret = EntityUtils.toString(response.getEntity(), "UTF-8"); 
      System.out.println("Response: "+ret); 
      System.out.println("#############################################"); 

     } 
     }catch(SocketTimeoutException e){ 
      if(Constants.DEBUG_MODE){ 
       Log.d(Constants.TAG, "SocketTimeoutException: " + e.toString()); 
      } 
     }catch(Exception e){ 
      //e.printStackTrace(); 
      if(Constants.DEBUG_MODE){ 
       Log.d(Constants.TAG, "Exception: " + e.toString()); 
      } 
     } 
     return ret; 
    } 

我getHTTPSClient()

public static DefaultHttpClient getHTTPSClient() { 
     SchemeRegistry schemeRegistry = new SchemeRegistry(); 
     schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
     schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); 

     HttpParams params = new BasicHttpParams(); 
     params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); 
     params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30)); 
     params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); 

     HttpConnectionParams.setConnectionTimeout(params, Constants.DEFAULT_CONNECTION_TIMEOUT); 
     HttpConnectionParams.setSoTimeout(params, Constants.DEFAULT_SOCKET_TIMEOUT); 
     ConnManagerParams.setTimeout(params, Constants.DEFAULT_SOCKET_TIMEOUT); 
     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 

     ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry); 
     DefaultHttpClient httpClient= new DefaultHttpClient(cm, params); 
     return httpClient; 
    } 

而且在活動(片段)我這樣打電話

result = Utils.callWS(Constants.getSP_WS_URL(), Constants.MODULE_MA, Constants.METHOD_POST_FILTER_OVERSEAS_POSTAL_INFO, payload); 

一個possile端點:

https://prdesb1.singpost.com/ma/FilterOverseasPostalInfo 

我檢查的有效載荷是正確的:

<OverseasPostalInfoDetailsRequest xmlns="http://singpost.com/paw/ns"><Country>AFAFG</Country><Weight>100</Weight><DeliveryServiceName></DeliveryServiceName><ItemType></ItemType><PriceRange>999</PriceRange><DeliveryTimeRange>999</DeliveryTimeRange></OverseasPostalInfoDetailsRequest> 

任何人都知道可能錯誤的,我們該如何處理呢?非常感謝

+0

您能張貼更多的錯誤信息嗎? – Henry

回答

1

你可以嘗試使用Volley或類似的庫來完成你的任務。比舊的方法更簡單。您大多需要重新編寫Web服務調用,但我可以保證它不會像您已經完成的那樣困難! Here's關於如何使用Volley的教程。開始很容易。

以下是在他們的網站上提供的關於如何使用Volley的簡單示例。 https://developer.android.com/training/volley/requestqueue.html

RequestQueue mRequestQueue; 

// Instantiate the cache 
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap 

// Set up the network to use HttpURLConnection as the HTTP client. 
Network network = new BasicNetwork(new HurlStack()); 

// Instantiate the RequestQueue with the cache and network. 
mRequestQueue = new RequestQueue(cache, network); 

// Start the queue 
mRequestQueue.start(); 

String url ="http://www.example.com"; 

// Formulate the request and handle the response. 
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
     new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 
     // Do something with the response 
    } 
}, 
    new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      // Handle error 
    } 
}); 

// Add the request to the RequestQueue. 
mRequestQueue.add(stringRequest); 
+0

謝謝,請深入研究。 Volley和Retrofit之間有什麼偏好? –

+0

您好我已經嘗試使用凌空重建我的API調用,但仍然有相同的錯誤。可以幫我看看。非常感謝http://stackoverflow.com/questions/39078888/volley-android-javax-net-ssl-sslhandshakexception-handshake-failed –

+0

嗨,一個可能的問題可能是由於您的服務器上的SSL證書配置問題。你可以在沒有https的情況下嘗試一樣嗎嘗試只使用http而不是https。我使用在線https://www.ssllabs.com/ssltest工具來查看您網站的SSL狀態。從結果來看,似乎沒有問題。但似乎有一些安全問題無論如何都應該解決。我建議你首先嚐試沒有SSL的情況,並讓我知道。 謝謝。 –