您好我正在处理一个使用xxd -i cer.pem | sed -e 's/unsigned char .* = {/const unsigned char OEM_KEYSTORE[] = {/g' -e 's/unsigned int .* = .*;//g'
类的旧Android项目,该项目现已折旧并将从API 22中删除。我们如何迁移到org.apache.httpclient
但仍保留原始功能? ALl我的http POST现在无法处理错误
httpUrlConnection
这是我的webservice API调用:
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
我的getHTTPSClient()
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;
}
在活动(片段)中,我这样称呼
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);
我检查的有效负载是正确的:
https://prdesb1.singpost.com/ma/FilterOverseasPostalInfo
任何人都知道错误的可能性以及我们如何处理它?非常感谢
答案 0 :(得分: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);