我正在android中处理HTTP post。这是我的代码:
SendMail sending = (SendMail) new SendMail()
.execute(url + "/mob/android/dummy.php");
public class SendMail extends AsyncTask<String, String, String> {
private String endResult;
@Override
protected String doInBackground(String... val) {
String url = val[0];
Log.i("Url", url + "");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
CookieStore cookieStore = new BasicCookieStore();
((AbstractHttpClient) httpclient).setCookieStore(cookieStore);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", post_name));
nameValuePairs
nameValuePairs.add(new BasicNameValuePair("email", post_email));
nameValuePairs.add(new BasicNameValuePair("message",
"Requesting appointment from: " + SelectedDr
+ " Message: " + post_message));
Log.i("nameValuePairs:", "" + nameValuePairs);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
BasicResponseHandler myHandler = new BasicResponseHandler();
endResult = myHandler.handleResponse(response);
Log.i("nameValuePairs", "" + nameValuePairs);
Log.i("endResult", "" + endResult);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
publishProgress(endResult);
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
String result = values[0];
T_warning.setText("Mail Sent.");
ET_email.setText("");
ET_name.setText("");
ET_message.setText("");
}
}
现在直到
nameValuePairs.add(new BasicNameValuePair("message",
"Requesting appointment from: " + SelectedDr
+ " Message: " + post_message));
它工作正常,但在 HttpResponse response = httpclient.execute(httppost); 3分钟后它没有回复它发送邮件但在我的邮件服务器上我没有收到邮件并且还显示异常:
org.apache.http.NoHttpResponseException:目标服务器响应失败
我被困住了,我不明白为什么会这样我添加了所有权限
日志猫:
04-05 10:41:57.110: I/exception(3926): org.apache.http.NoHttpResponseException: The target server failed to respond
04-05 10:41:57.110: I/endResult(3926): null
编辑后的代码给出了 LOGCAT:
04-05 10:51:17.160: W/System.err(4016): org.apache.http.NoHttpResponseException: The target server failed to respond
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
04-05 10:51:17.170: W/System.err(4016): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
04-05 10:51:17.170: W/System.err(4016): at com.ilmasoft.alina_dental.MailUs$SendMail.doInBackground(MailUs.java:492)
04-05 10:51:17.180: W/System.err(4016): at com.ilmasoft.alina_dental.MailUs$SendMail.doInBackground(MailUs.java:1)
04-05 10:51:17.180: W/System.err(4016): at android.os.AsyncTask$2.call(AsyncTask.java:252)
04-05 10:51:17.180: W/System.err(4016): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-05 10:51:17.180: W/System.err(4016): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-05 10:51:17.180: W/System.err(4016): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
04-05 10:51:17.180: W/System.err(4016): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
04-05 10:51:17.180: W/System.err(4016): at java.lang.Thread.run(Thread.java:1020)