2013-02-28 69 views
0

我必須在類似這樣的URL發送數據:用郵寄方式發送數據URI

http://webservices.fecth.es/school/message/sendmessage/ID/myStringId/目的地/myStringDestination/味精/myMessage

我在字符串中有必要的數據,但不是通過post方法發送的,我測試過這段代碼但不起作用:

String postURL = "http://webservices.fecth.es/school/message/sendmessage/"; 
HttpPost post = new HttpPost(postURL); 
List<NameValuePair> param = new ArrayList<NameValuePair>(); 
param.add(new BasicNameValuePair("id", myId)); 
param.add(new BasicNameValuePair("destination", myStringDestination)); 
param.add(new BasicNameValuePair("msg", myMessage)); 

UrlEncodedFormEntity ent = new UrlEncodedFormEntity(param,HTTP.ISO_8859_1); 
post.setEntity(ent); 
HttpResponse responsePOST = client.execute(post); 
HttpEntity resEntity = responsePOST.getEntity(); 

他們可以幫我解決我的問題嗎?

非常感謝

+0

不追加它作爲一個PARAM。簡單地附加它的網址。 – 2013-02-28 14:32:24

+0

Rajesh建議正確的評價... – 2013-02-28 14:32:44

回答

1

試試這個:

StringBuffer postURL = new StringBuffer("http://webservices.fecth.es/school/message/sendmessage/"); 
postURL.append("id/"); 
postURL.append(URLEncoder.encode(myId, "UTF-8")); 
postURL.append("/destination/"); 
postURL.append(URLEncoder.encode(myStringDestination, "UTF-8")); 
postURL.append("/msg/"); 
postURL.append(URLEncoder.encode(myMessage, "UTF-8")); 

HttpPost post = new HttpPost(postURL.toString()); 
HttpResponse responsePOST = client.execute(post); 
HttpEntity resEntity = responsePOST.getEntity(); 
+0

感謝您的答案,我試過代碼非常興奮,我認爲這是我需要的。但它沒有工作,我可以看到已創建的網址發送?有沒有可能最終的網址在這裏? 'postURL.toString()' – jlopez 2013-02-28 14:53:21

+0

是的,使用Log語句調試或打印它來檢查內容。另外,當你說「它沒有工作」時,請說明你面臨的錯誤到底是什麼。 – Rajesh 2013-02-28 14:55:55

+0

適合我,謝謝! – jlopez 2013-02-28 15:55:13

0

請檢查與 狀態下面的代碼

responsePOST.getStatusLine().getStatusCode() 
+0

我得到的價值200 – jlopez 2013-02-28 14:36:25