【问题标题】:the request url cannot be retrieve?无法检索请求网址?
【发布时间】:2011-03-22 11:56:19
【问题描述】:

我已经使用 HttpPost 从 android 向 PHP 发送值 但我得到响应“无法检索请求 url”

这是我的代码

try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(link);
            post.setEntity(new UrlEncodedFormEntity(sendValue));
            HttpResponse response = client.execute(post);
            Toast.makeText(this, response.getStatusLine().toString(),
                    Toast.LENGTH_LONG).show();
            if (response.getStatusLine().toString().contains("200")) {
                Toast.makeText(this, "Komentar berhasil dibuat",
                        Toast.LENGTH_LONG).show();
                finish();
            }
            else{
                Toast.makeText(this, "Koneksi server bermasalah",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Log.e("log_tag", "Error connection" + e.toString());
        }

但如果我删除“post.setEntity(new UrlEncodedFormEntity(sendValue));” 我可以连接到 PHP 文件..

如何解决?

编辑
我是这样设置的

private List<NameValuePair> sendValue = new ArrayList<NameValuePair>(4);
sendValue.add(new BasicNameValuePair("link", urlShare.toString()));
sendValue.add(new BasicNameValuePair("message", postComment.getText().toString()));
sendValue.add(new BasicNameValuePair("name", nameText.getText().toString()));
sendValue.add(new BasicNameValuePair("email", emailText.getText().toString()));

【问题讨论】:

  • 你在清单文件中设置了网络权限吗?
  • 是的,我已经在清单文件中设置了访问 INTERNET 的权限..

标签: php android httpclient http-post


【解决方案1】:

检查您放入sendValue 的值是否正确编码。您可能还需要在 post 对象上设置内容类型:

post.setHeader("Content-Type","application/x-www-form-urlencoded");

【讨论】:

  • 感谢戴夫的回复。我已经按照你的建议尝试过,但我仍然无法检索网址。。
【解决方案2】:

正如 dave.c 所说,很可能您的问题在于编码。试试

String urlShareStr = URLEncoder.encode(urlShare.toString());
sendValue.add(new BasicNameValuePair("link", urlShareStr);

其余部分相同。我只是为了更容易阅读而分成两个命令。您可以将其压缩为

sendValue.add(new BasicNameValuePair("link", URLEncoder.encode(urlShare.toString());

【讨论】:

    猜你喜欢
    • 2012-11-09
    • 2017-04-03
    • 2017-04-25
    • 1970-01-01
    • 2017-11-16
    • 2023-03-12
    • 2018-08-21
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多