2016-11-21 76 views
0

我有我的Web服務器使用一些POST方法的Android應用程序。在3天之前,我的應用程序在每個Android設備上都能順利運行。今天我遇到了問題,我的https POST調用只適用於Android設備6.0。優化版本。我有更多的設備(5.0和4.3.x),並且在他們的同一個應用程序不能建立與我的WEB服務器的通信。我已經和託管我的Web服務器的託管公司談過了,他們告訴我他們在過去三天沒有改變過。奇怪的是,相同的代碼在api 23上工作,但不是比api 23(測試5.0和4.3.x)少。所以我的問題是,在過去的三天裏有什麼變化,我沒有注意到來自android的https調用或什麼?Android POST方法停止工作

我製作代碼HTTPS從Android的調用是下面(的AsyncTask)

protected String doInBackground(Object... params) { 
    String param1="a"; 
    String param2 = "b"; 
    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("http", socketFactory, 80)); 
    registry.register(new Scheme("https", socketFactory, 443)); 
    SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry); 
    DefaultHttpClient httpclient = new DefaultHttpClient(mgr, client.getParams()); 

    // Set verifier 
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); 

    // Example send http request 
    final String url = Config.url_login_async_task; 
    HttpPost httppost = new HttpPost(url); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("param1", param1)); 
     nameValuePairs.add(new BasicNameValuePair("param2", param2)); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     // Execute HTTP Post Request 
     response = httpclient.execute(httppost); 

    } catch (ClientProtocolException e) { 
    } catch (IOException e) { 
    } 
    try { 
     //Log.d("response", response.toString()); 
     odgovor = EntityUtils.toString(response.getEntity()); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return odgovor; 
} 

我有點困惑,因爲應用程序停止在某些設備上正常工作,對一些不是,我想知道這是爲什麼。

編輯:加入的gradle項目

buildscript { 
repositories { 
    jcenter() 
} 
dependencies { 
    classpath 'com.android.tools.build:gradle:1.3.0' 

    // NOTE: Do not place your application dependencies here; they belong 
    // in the individual module build.gradle files 
    } 
} 

allprojects { 
repositories { 
    jcenter() 
} 
} 

模塊搖籃

apply plugin: 'com.android.application' 

android { 

    compileSdkVersion 23 
    buildToolsVersion "23.0.1" 
    packagingOptions { 
     exclude 'META-INF/DEPENDENCIES.txt' 
     exclude 'META-INF/DEPENDENCIES' 
     exclude 'META-INF/dependencies.txt' 
     exclude 'META-INF/LICENSE.txt' 
     exclude 'META-INF/LICENSE' 
     exclude 'META-INF/license.txt' 
     exclude 'META-INF/LGPL2.1' 
     exclude 'META-INF/NOTICE.txt' 
     exclude 'META-INF/NOTICE' 
     exclude 'META-INF/notice.txt' 
    } 
    defaultConfig { 
     applicationId "com.x.y.z" 
     minSdkVersion 15 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 
dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    compile 'com.android.support:appcompat-v7:23.0.1' 
    compile 'com.google.android.gms:play-services:7.8.0' 
} 

編輯2:

嘗試添加useLibrary 'org.apache.http.legacy'android {}和所有其他soluti來自其他環節,仍然沒有成功。我讀到了這個「棄用的」apache庫,奇怪的是,在低於API 22的設備上,我的應用程序崩潰了(它應該工作,因爲在那些api lvls上它應該工作),並且在API 23上它工作(並且它不應該因爲在API 23它棄用

編輯3:新的錯誤

仔細調試和所花時間試圖找到的錯誤,我拿出了一個錯誤在我的日誌後

無法設置EGL_SWAP_BEHAVIOR在表面0xa379c740上,錯誤= EGL_SUCCESS javax.net.ssl.SSLPeerUnverifiedException:沒有對等證書 at com.android.org.conscrypt.SSLNullSession.getPeerCertificates(SSLNullSession.java:104) at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:98) at org.apache.http .conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:393) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:170) at org.apache.http.impl.conn.AbstractPoolEntry .Open(AbstractPoolEntry.java:169) at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java :365) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560) 在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492) 在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)

有人可以嘗試向我解釋發生此錯誤的證書在服務器上發生了什麼嗎?

+0

也許http://stackoverflow.com/問題/ 29294479/android-deprecated-apache-module-httpclient-httpresponse -etype – TWL

+0

你能否解釋爲什麼應用程序在沒有任何代碼更改的情況下停止工作? – KuKeC

+1

由於可能的棄用。我建議你看看網絡庫如Volley,Retrofit和OkHttp – Chisko

回答

1

此問題基於HTTPS:拋出:SecurityException在服務器上,一旦託管域更新您的SSL,U將得到正確的響應,否則你也會使用下面的代碼

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; 
DefaultHttpClient httpClient = new DefaultHttpClient(); 
SchemeRegistry registry = new SchemeRegistry(); 
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory(); 
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier); 
registry.register(new Scheme("https", socketFactory, 443)); 
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
SingleClientConnManager mgr = new SingleClientConnManager(httpClient.getParams(), registry); 
DefaultHttpClient client = new DefaultHttpClient(mgr, httpClient.getParams()); 

// Set verifier 
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); 

DefaultHttpClient client=new DefaultHttpClient(); 
HttpPost request = new HttpPost(url); 
request.setEntity(new StringEntity(jobject.toString())); 
request.setHeader("Content-type", "application/json"); 
HttpResponse response = client.execute(request); 
HttpEntity resEntity = response.getEntity(); 
String json = EntityUtils.toString(resEntity); 
+0

不,請告訴我如何在cpanel上重新安裝ssl? – KuKeC

+0

請參閱此鏈接https://www.namecheap.com/support/knowledgebase/article.aspx/9418/0/cpanel –