2013-01-03 16 views
-1
HTTPS URL JSON響應

我正在從HTTPS JSON響應並獲得異常作爲異常而獲取針對Android

javax.net.ssl.SSLException: Not trusted server certificate 
下面

是我的代碼,我是否嘗試用HTTPGET或HttpPost我得到的異常在行

HttpResponse httpResponse = httpClient.execute(request); 

這些是從URL獲取數據的代碼。

DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost request = new HttpPost(urlString); 
     HttpResponse httpResponse = httpClient.execute(request); 

希望能在這個問題上得到答案。謝謝。

+0

請參閱[this](http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html)使用DefaultHttpClient –

+0

進行https請求不是json響應。沒有堆棧跟蹤。你的服務器使用你的設備不信任的證書,這就是錯誤所說的。 – njzk2

回答

0

錯誤表示無法建立服務器證書的有效性。可能有幾個原因,例如,受信任的根證書不可用。

你能從你的瀏覽器連接到網頁?

0

我,同時爲我校的應用程序也有類似的問題。訣竅是創建兩個覆蓋證書驗證的類。一個擴展爲HostnameVerifier,每次調用verify()時返回true,如t this。其他class extends X509TrustManager並重寫getAcceptedIssuers(),如this

然後你可以設置HttpsURLConnection的使用此代碼

HttpsURLConnection.setDefaultHostnameVerifier(new AllVerifier()); 
try 
    { 
    SSLContext sslContext = SSLContext.getInstance("TLS"); 
    sslContext.init(null, new TrustManager[] { new AllTrustManager() }, null); 
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); 
    } catch (KeyManagementException e) { 
     e.printStackTrace(); 
    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } 

這應該做的伎倆,以接受所有證書。你可以在run()方法中看到我在這裏如何使用this代碼。