Reputation: 2531
I am working on HTTP post in android. Here is my code:
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("");
}
}
Now till
nameValuePairs.add(new BasicNameValuePair("message",
"Requesting appointment from: " + SelectedDr
+ " Message: " + post_message));
it is working fine but on HttpResponse response = httpclient.execute(httppost); it gives no reponse after 3 minutes it gives mail sent but on my mail server I received no mail and also show exception:
org.apache.http.NoHttpResponseException: The target server failed to respond
I am stuck with that I could not understand why this is happening I added all permissions
LOGCAT:
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
Edited Code gives 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)
Upvotes: 1
Views: 3928
Reputation: 2528
try this code within doInBackground
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", post_name));
nameValuePairs.add(new BasicNameValuePair("email", post_email));
nameValuePairs.add(new BasicNameValuePair("message",
"Requesting appointment from: " + SelectedDr
+ " Message: " + post_message));
DefaultHttpClient hc = new DefaultHttpClient();
ResponseHandler<String> res = new BasicResponseHandler();
HttpPost postMethod = new HttpPost(url);
String response = null;
try {
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
response = hc.execute(postMethod, res);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 1