Java 在SDK 2.2以上,HTTP Post从不响应(冻结)
所以我已经在安卓应用上工作了一段时间了。一切都很顺利,直到我在安卓2.2以上的设备上测试了这段代码 我只是在一个异步任务中将数据发布到我的PHP服务器上,它永远不会完成,没有错误,什么都没有。我试图设置超时,但没有成功。我在try{}catch{}块中放入了每一条不同的语句,但没有一条语句触发。通过在互联网上阅读,该bug似乎位于HTTPClient.execute(httppost)序列的某个地方。这是我的密码:Java 在SDK 2.2以上,HTTP Post从不响应(冻结),java,php,android,html,http,Java,Php,Android,Html,Http,所以我已经在安卓应用上工作了一段时间了。一切都很顺利,直到我在安卓2.2以上的设备上测试了这段代码 我只是在一个异步任务中将数据发布到我的PHP服务器上,它永远不会完成,没有错误,什么都没有。我试图设置超时,但没有成功。我在try{}catch{}块中放入了每一条不同的语句,但没有一条语句触发。通过在互联网上阅读,该bug似乎位于HTTPClient.execute(httppost)序列的某个地方。这是我的密码: private class NewUserTask extends AsyncT
private class NewUserTask extends AsyncTask<Context, Void, String> {
@Override
protected String doInBackground(Context... params) {
Functions functions = new Functions();
if (!functions.isOnline(params[0])) {
return "false";
}
String result = null;
InputStream is = null;
StringBuilder sb = null;
String userDeviceID = Secure.getString(params[0].getContentResolver(),Secure.ANDROID_ID);
String userAccount = getUsername(params[0]);
HttpClient httpclient = null;
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
httpclient = new DefaultHttpClient(httpParameters);
}catch (Exception e) {
return "error httpclient setup";
}
String url = null;
try {
url = "http://www.xxx.com/xxx/xxx.php?userDeviceID=" + userDeviceID + "&database=" + "xxx" + "&userAccount=" + userAccount;
}catch (Exception e) {
return "url setup";
}
HttpPost httppost = null;
try {
httppost = new HttpPost(url);
}catch (Exception e) {
return "httppostsetup";
}
try {
httppost.setEntity(new UrlEncodedFormEntity(new ArrayList()));
}catch (Exception e) {
return "setetnityfsda";
}
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
}catch (Exception e) {
return "response";
}
try {
is = response.getEntity().getContent();
}catch (Exception e) {
return "getcontent";
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"));
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String temp = sb.toString();
temp = temp.substring(0, temp.length() - 1);
result = temp;
}catch (Exception e) {
return "error in reader";
}
return result;
}
protected void onPostExecute(String result) {
Functions functions = new Functions();
functions.showMessage("Info", result, DisplayExtrasActivity.this);
}
private String getUsername(Context context){
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(context).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
return account.name;
}
}
return null;
}
}
我真的希望有人能帮助我,因为我几乎绝望了。我花了一个又一个小时在这些问题上,试图找出我错在哪里
编辑:
更多代码 为什么您没有使用2.2以上的设备进行测试?我们已经进入2014年了!您能发布logcat错误吗?您能同时显示
AsyncTask
的完整代码吗?创建任务后,您是否执行了该任务?
new NewUserTask().execute(DisplayExtrasActivity.this);