2011-03-22 158 views
0

我有送价值从机器人到PHP和HttpPost 但我得到响应“请求的URL不能被检索”请求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(新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())); 
+0

什么是'sendValue'设置? – 2011-03-22 12:14:32

+0

您是否在清单文件中设置了网络权限? – Olegas 2011-03-22 13:45:01

+0

是的,我已设置权限访问互联网在清单文件.. – pensilhijau 2011-03-22 15:01:25

回答

0

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

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

感谢戴夫为您的答复..我已经尝试过像你的建议,但我仍然无法检索的网址.. – pensilhijau 2011-03-23 03:21:22

0

正如dave.c说,最有可能你rproblem是编码。尝试

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

和其余的一样。为了便于阅读,我将它分成了两个命令。你可以把它凝结

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

+0

好吧,谢谢爱达荷,我会试试它.. – pensilhijau 2011-03-23 03:39:12