2012-07-05 28 views
1

嘗試將我的android應用程序轉換爲使用SSL在我的android應用程序和Web服務器之間傳輸信息後,出現一些問題。 (SocketTimeOutException)在android應用程序中將http轉換爲https後的SocketTimeoutException

我已從證書頒發機構(CA)購買了Positive SSL證書,並配置了我的服務器以正確使用它。我已經在我的網絡瀏覽器中測試過它,並且它的工作正常。

現在我試圖修改我的android應用程序使用https而不是http,但由於這是我第一次使用https自己,我有點困惑,我需要在java代碼中實現哪些步驟。

目前我正在設置我的設置頁面,在該頁面中我將應用程序配置爲使用正確的URL。例如,我在一個活動中有2個文本字段,我在其中輸入網站URL(http://www.mydomain.com)和相關頁面存儲在的應用程序文件夾(myfolfolder)

然後我使用以下代碼連接和測試連接變量配置正確,其中validateurl.aspx是,如果存在的頁面,返回一個JSON字符串的網頁:

protected boolean validateConnectionSettings() {   

    String result = ""; 
    boolean validated = false; 
    try { 
     StringBuilder urlBuilder = new StringBuilder(); 
     urlBuilder.append(tvWebsiteURLValue.getText() + File.separator + tvApplicationMiddlewareValue.getText() + File.separator + "validateurl.aspx"); 
     URL url = new URL(urlBuilder.toString()); 
     URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); 
     url = uri.toURL(); 

     URLConnection urlConn = url.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 

     String inputLine; 
     while((inputLine = in.readLine()) != null){ 
      result += inputLine; 
     }     
     in.close(); 

     if(result.equals("exists")) { 
      validated = true; 
     } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.e(TAG, e.getMessage()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.e(TAG, e.getMessage()); 
    } 

    return validated; 
} 

現在,當我嘗試轉換(HTTP://www.mydomain .com)變量來使用https我得到了上面提到的java.net.SocketTimeoutException。

我用Google搜索這個問題,並看到了,我要在上面的代碼來實現的,而不是HttpsURLConnection的URLConnection的所以我相應的修改我的代碼如下:

protected boolean validateConnectionSettings() {   

    String result = ""; 
    boolean validated = false; 
    try { 
     StringBuilder urlBuilder = new StringBuilder(); 
     urlBuilder.append(tvWebsiteURLValue.getText() + File.separator + tvApplicationMiddlewareValue.getText() + File.separator + "validateurl.aspx"); 
     URL url = new URL(urlBuilder.toString()); 
     URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); 
     url = uri.toURL(); 

     // URLConnection urlConn = url.openConnection(); 
     HttpsURLConnection urlConn = (HttpsURLConnection)url.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 

     String inputLine; 
     while((inputLine = in.readLine()) != null){ 
      result += inputLine; 
     }     
     in.close(); 

     if(result.equals("exists")) { 
      validated = true; 
     } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.e(TAG, e.getMessage()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.e(TAG, e.getMessage()); 
    } 

    return validated; 
} 

不過我還是收到了java.net .SocketTimeoutException。任何想法我做錯了什麼?

回答

1

好吧,我發現導致SocketTimeOutException的問題。

看來問題在於我正在家中工作,手機正在使用與服務器所在的網絡相同的網絡。在這種情況下,似乎無法使用https訪問服務器。我關閉了手機無線網絡,並使用手機3G連接進行連接,並且嘿pre the應用連接並驗證了我的網址。

我有類似的問題,直接從服務器和我的工作筆記本電腦在Web瀏覽器中測試https連接。在每種情況下,我通過將www.mydomain.com地址添加到主機文件中來解決問題。

我現在要看看編輯我的Android手機主機文件,看看這是否會解決它,因爲我不想用手機G3連接進行測試,因爲我期望病得不償失......

生病後評論下方後來說是怎麼一回事呢

我希望這可以幫助其他人也有類似的問題,使用下面的鏈接生根我的手機後http://www.androidauthority.com/how

+0

OK -to-root-your-samsung-galaxy-ace-37385 /然後從google play上下載並使用應用主機編輯器。我能夠編輯我的主機文件並添加我的網絡服務器的IP和別名。 這工作很好,現在我可以在我的內部網絡上使用https連接到網絡服務器 – 2012-07-06 14:59:48

相關問題