我有一个Android应用程序,它从两个webservices获得响应。用PHP编写的webservices。 Web服务托管在Live服务器中,我的请求将是HTTP和HTTPS.Below代码运行良好 Android 5.0,6.0,7.0,我用4.4.2测试也很好用。 但是当我尝试在Android 4.3中运行我的应用程序时,我的应用程序将崩溃。
我尝试过的,
try{
String url = dRequestParam.getURL();
HttpEntity httpEntity;
HttpResponse httpResponse;
DefaultHttpClient httpClient;
if(url.startsWith("http://")){
//http client
httpClient = new DefaultHttpClient();
httpEntity = null;
httpResponse = null;
}else{
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
httpClient = new DefaultHttpClient(mgr, client.getParams());
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
int AdFetchTimeout=Integer.parseInt(AdView.getFetchTimeout());
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, AdFetchTimeout);
httpEntity = null;
httpResponse = null;
}
//checking http request method type
if(method == POST){
HttpPost httpPost = new HttpPost();
//adding post params
if(params != null){
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
}else if(method == GET){
//appending params to url
params = AdRequestParam.getURL_PARAMS();
if(params != null){
String paramString = URLEncodedUtils.format(params,"utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
// SET HEADERS PARAMETERS
hParams = AdRequestParam.getHEADER_PARAMS();
for (NameValuePair nvp : hParams) {
String name = nvp.getName();
String value = nvp.getValue();
Cdlog.d(Cdlog.httpReqLogTag,name+"=>"+value);
httpGet.setHeader(name,value);
}
Long startTime = null;
try {
startTime = System.currentTimeMillis();
httpResponse = httpClient.execute(httpGet);
Cdlog.d(Cdlog.httpReqLogTag,url);
Cdlog.setLastRequest(url);
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
Cdlog.d(Cdlog.httpResLogTag,response);
Cdlog.setLastResponse(response);
}
catch(SocketTimeoutException se) {
Long endTime = System.currentTimeMillis();
se.printStackTrace();
}
catch(ConnectTimeoutException cte) {
Long endTime = System.currentTimeMillis();
response="{\"ad_available\":\"5\",\"ad_tag\":\"<div></div>\"}";
cte.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
Long endTime = System.currentTimeMillis();
e.printStackTrace();
}
//DAC125 Timeout Mechanism
}
}catch (UnsupportedEncodingException e){
Log.d("Unsupported Encoding Exception",e.getMessage());
e.printStackTrace();
} catch (ClientProtocolException e){
Log.d("Client Protocol Exception",e.getMessage());
e.printStackTrace();
} catch (IOException e){
Log.d("IO Exception",e.getMessage());
e.printStackTrace();
} catch(SecurityException e){
Log.d("Security Exception",e.getMessage());
//response = e.getMessage();
e.printStackTrace();
}
return response;
}
logcat的:
01-18 10:49:22.552 6417-6417/? W/System.err: java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Scheme 'http' not registered.
63. 01-18 10:49:22.552 6417-6479/? W/dalvikvm: threadid=12: thread exiting with uncaught exception (group=0x415a1700)
64. 01-18 10:49:22.552 6417-6417/? W/System.err: at java.util.concurrent.FutureTask.report(FutureTask.java:94)
65. 01-18 10:49:22.552 6417-6417/? W/System.err: at java.util.concurrent.FutureTask.get(FutureTask.java:160)
66. 01-18 10:49:22.552 6417-6417/? W/System.err: at android.os.AsyncTask.get(AsyncTask.java:482)
67. 01-18 10:49:22.552 6417-6417/? W/System.err: at com.vrtcal.openfalls.adserver.AdFetcher$RequestHandler.handleMessage(AdFetcher.java:230)
68. 01-18 10:49:22.552 6417-6417/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
69. 01-18 10:49:22.552 6417-6417/? W/System.err: at android.os.Looper.loop(Looper.java:137)
70. 01-18 10:49:22.552 6417-6417/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5103)
71. 01-18 10:49:22.552 6417-6417/? W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
72. 01-18 10:49:22.552 6417-6417/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:525)