【问题标题】:Always receive org.apache.http.NoHttpResponseException: The target server failed to respond Android总是收到 org.apache.http.NoHttpResponseException: The target server failed to respond Android
【发布时间】:2013-04-05 05:27:40
【问题描述】:

我正在 Android 中处理 HTTP 帖子。这是我的代码:

 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: 目标服务器响应失败

我不明白为什么会这样我添加了所有权限

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

编辑后的代码给出 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)

【问题讨论】:

  • 在应用程序崩溃时发布带有问题的 logcat 结果以获得我们的更多帮助
  • 查看我编辑的问题
  • 我在询问整个日志而不是其中的两行。或者如果您无法共享整个日志,则只需从 logcat 共享 Log.i("Url", url + "");
  • 网址:www.dummy.com/mob/android/dummy.php

标签: android email http-post


【解决方案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();
            }

【讨论】:

  • 我试过你的代码,它给出了异常。请查看我编辑的问题
  • 您确定您的清单中添加了互联网权限吗?如果是,请重新检查您正在执行的网址
  • 我无法分享实际的网址,但我正在执行这个网址:dummy.com/mob/android/dummy.php
  • 我在 samsung duos 中检查了相同的代码及其工作,但是当我在 10.1 选项卡上运行它时,它给了我系统错误
  • 看看这个link
猜你喜欢
  • 2016-08-30
  • 2021-12-09
  • 1970-01-01
  • 2022-12-27
  • 2021-10-13
  • 2021-12-10
  • 1970-01-01
  • 2023-02-20
  • 2020-09-18
相关资源
最近更新 更多