可能重複:
Android app doesn’t work on Android 4HttpURLConnection的連接上4.0和4.1,但不()異常2.3.3
我工作的一個Android應用程序。試圖找到幾個小時的解決方案。當我運行模擬器2.3.3時,我的應用可以正常運行數據庫和服務器,但是在4.0或4.1或2.3.3以上的任何版本上都沒有問題。該應用程序在此方法中引發異常:嘗試執行connection.connect();
時出現異常,請幫助。
public void sendOutputLine(URL url, String outputLine) {
try {
connection = (HttpURLConnection) url.openConnection();
Log.d(LogInActivity.DEBUG_TAG, "1");
connection.setDoInput(true);
Log.d(LogInActivity.DEBUG_TAG, "2");
connection.setDoOutput(true);
Log.d(LogInActivity.DEBUG_TAG, "3");
connection.setRequestMethod("POST");
Log.d(LogInActivity.DEBUG_TAG, "4");
connection.setFixedLengthStreamingMode(outputLine.length());
Log.d(LogInActivity.DEBUG_TAG, "5");
connection.connect();
Log.d(LogInActivity.DEBUG_TAG, "6");
out = connection.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
pw.print(outputLine);
pw.flush();
out.close();
} catch (SocketTimeoutException e) {
Log.d(LogInActivity.DEBUG_TAG,
"SocketTimeoutException sendOutputLine(): " + e.getMessage());
} catch (ProtocolException e) {
Log.d(LogInActivity.DEBUG_TAG,
"Protocol exception sendOutputLine(): " + e.getMessage());
} catch (IOException e) {
Log.d(LogInActivity.DEBUG_TAG, "IO exception sendOutputLine(): "
+ e.getMessage());
} catch (Exception e) {
Log.d(LogInActivity.DEBUG_TAG,
"Exception bommed client: " + e.getMessage());
}
}
的logcat:
11-17 13:19:11.710: D/SocialFamilyTree(847): outputLint: Login%[email protected]%57963634
11-17 13:19:11.710: D/SocialFamilyTree(847): 1
11-17 13:19:11.710: D/SocialFamilyTree(847): 2
11-17 13:19:11.710: D/SocialFamilyTree(847): 3
11-17 13:19:11.710: D/SocialFamilyTree(847): 4
11-17 13:19:11.710: D/SocialFamilyTree(847): 5
11-17 13:19:11.740: D/SocialFamilyTree(847): Exception bommed client: null
編輯: 我明白了,所以我需要使用AsyncTask
如何使用AsyncTask
完成這個任務?通過這種方式
** Utils **來自哪裏? –
這是類......我宣佈了進度對話框的顯示和隱藏功能 –