2014-10-17 37 views
0

在我的應用程序中,我通過HttpURLConnection向服務器發送POST請求。但因爲我更新了我的Samsung S4到Android 4.4.2,我沒有收到服務器的任何請求。Android 4.4.2 Kitkat無法通過HttpURLConnection發送請求

這裏是我的代碼:

private static void send (final String uri, String SomeText) throws IOException { 
    final URL url = new URL (uri); 
    final HttpURLConnection conn; 

    // setup the connection 
    conn = (HttpURLConnection) url.openConnection(); 
    conn.setDoInput (true); 
    conn.setDoOutput (true); 
    conn.setUseCaches (false); 
    conn.setRequestMethod ("POST"); 
    conn.setChunkedStreamingMode (0); 

    final OutputStream os = conn.getOutputStream(); 
    final OutputStreamWriter osw = new OutputStreamWriter (os); 
    final BufferedWriter writer = new BufferedWriter (osw); 

    try { 

    writer.write (SomeText); 

    } finally { 
    writer.flush(); 
    writer.close(); 
    conn.disconnect(); 
    } 
} 

我做錯什麼了嗎? KitKat是否有任何HttpURLConnection更改?

當我使用DefaultHttpClient時,一切正常,我可以收到服務器的請求。但我仍然喜歡使用HttpURLConnection,因爲該應用程序使用這種連接進行測試。

回答

0

請注意是否使用模擬器。我使用Proxy時遇到了這樣的問題。 BTW。你有沒有添加到您的清單:

<uses-permission 
    android:name="android.permission.INTERNET"/> 

而且與響應使用相應的類 http://developer.android.com/reference/org/apache/http/HttpResponse.html

+0

的權限已經加入到這個應用程序的工作。該應用在Android 4.2.2上運行良好。 Android 4.4.2的版本更新會出現問題。順便說一句,服務器是獨立編程的另一種語言,並在更新android手機之前得到所有請求。 – partisan 2014-10-17 10:39:33

+0

很奇怪嘗試使用HTTP請求/響應而不是HTTPURLConnection可能會幫助... – Hunkeone 2014-10-17 11:19:00

相關問題