1

我正在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)
4

1 回答 1

1

在 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();
            }
于 2013-04-05T05:37:04.690 回答