2013-10-05 290 views
3

我想發送一個HTTP POST請求使用'form-data'。這裏是休息的客戶端的截圖,顯示做我想做的:發送表單數據發佈請求使用Http Post在android

enter image description here

更多相關的信息標題:

POST /action HTTP/1.1 
Host: requsrt-utl 
Cache-Control: no-cache 

----WebKitFormBoundaryE19zNvXGzXaLvS5C 
Content-Disposition: form-data; name="email" 

[email protected] 
----WebKitFormBoundaryE19zNvXGzXaLvS5C 
Content-Disposition: form-data; name="password" 

123456 
----WebKitFormBoundaryE19zNvXGzXaLvS5C 

我嘗試使用UrlEncodedFormEntity但設置內容類型爲'application/x-www-form-urlencoded',這是不正確的。

我的Android代碼:

HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(urlStr); 
     try { 
      UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(nameValuePairs); 
      httppost.setEntity(encodedFormEntity); 
      HttpResponse httpresponse = httpclient.execute(httppost); 
      return httpresponse.getEntity().getContent(); 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

時,我用這個,網絡服務沒有得到任何參數,因此它發送一個消息「的參數是不存在的」。

請幫忙!

回答

4

這可能會幫助你

HttpPost httppost = new HttpPost(urlStr); 

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 

nameValuePairs.add(new BasicNameValuePair("userid", "12312")); 
nameValuePairs.add(new BasicNameValuePair("sessionid", "234")); 

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

how to add parameters in android http post?

+0

我做相同的,但總是得到 '415個不支持的媒體' 類型。我甚至沒有設置任何內容類型標題。 – Darpan

+0

您可能需要設置標題'Content-Type'。 'request.addHeader(「Content-Type」,「application/x-www-form-urlencoded」);' – BlaShadow

+0

不需要內容頭。他們創建了一個多部分http調用,所以我做到了。 – Darpan