请求网址无法检索?

时间:2011-03-22 11:56:20

标签: php android httpclient http-post

我已经通过HttpPost从Android向PHP发送了价值 但我得到回应“请求网址无法检索”

这是我的代码

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()));

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

和其他人一样。我把它分成两个命令只是为了更容易阅读。你可以将它压缩到

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

相关问题