我正在开发一个安卓应用程序。试图找到解决方案几个小时。当我运行模拟器 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());
}
}
日志猫:
11-17 13:19:11.710: D/SocialFamilyTree(847): outputLint: Login%a@a%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
这个任务?