2011-03-22 125 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