2011-05-11 53 views
10

我需要使用參數創建HTTP POST請求。我知道有很多例子,我嘗試使用HTTPparams,NameValuePair等,但似乎無法得到正確的服務器格式。Android:參數不起作用的Http post

Server Type: REST based API utilizing JSON for data transfer
Content-type: application/json
Accept: application/json
Content-length: 47
{"username":"abcd","password":"1234"}

我可以通過這些信息,但是我似乎無法通過這些PARAMS「用戶名」,「密碼」。這裏是我的代碼:

HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login"); 
    List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
    pairs.add(new BasicNameValuePair("username","abcd")); 
    pairs.add(new BasicNameValuePair("password","1234")); 
    post.setHeader("Content-type", "application/json"); 
    post.setHeader("Accept", "application/json"); 
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,"UTF-8"); 
    post.setEntity(entity); 
    HttpResponse response = client.execute(post); 

我試圖調試,但不能看到,如果實體正確或不附...什麼是我做錯了什麼?

在此先感謝。 瑪斯

回答

15

試試這個:

HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login"); 
    post.setHeader("Content-type", "application/json"); 
    post.setHeader("Accept", "application/json"); 
JSONObject obj = new JSONObject(); 
obj.put("username", "abcd"); 
obj.put("password", "1234"); 
    post.setEntity(new StringEntity(obj.toString(), "UTF-8")); 
    HttpResponse response = client.execute(post); 
+0

我需要的!非常感謝。 :) – Maaz 2011-05-11 02:30:05

+3

完美 - 添加,我喜歡使用定義的常量:'HTTP.UTF_8' – 2013-02-06 13:27:28

+0

爲什麼這個工作和OP的代碼不?我面臨類似的問題,但只有10個服務器中有1個。 – 2014-10-24 04:12:53

1

我不太清楚,從你的描述,但它似乎您的服務器需要一個JSON內容對象,而不是數據在URL中被編碼。發送這樣的事情作爲您的帖子

{"username":"abcd","password":"1234"} 
+1

謝謝Dmon,Femis代碼工作正常。但即使你的建議會導致我到正確的代碼:) – Maaz 2011-05-11 02:26:30

1
HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login"); 
List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 

pairs.add(new BasicNameValuePair("username","abcd")); 
pairs.add(new BasicNameValuePair("password","1234")); 

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,HTTP.UTF_8); 
post.setEntity(entity); 
HttpResponse response = client.execute(post); 

只是試試這個怎麼把它的工作非常適合我,當我試圖HTTP發佈。

+0

謝謝蘇曼特,我曾嘗試在一個階段,但沒有工作。 :( – Maaz 2011-05-11 02:30:30

1

這可能會爲你工作。

假設你已經有了JSON對象。

注:(1)在你需要處理的請求日爲UTF-8(也在DB)服務器。

@SuppressWarnings("unchecked") 
private static HttpResponse executePostRequest(JSONObject jsonData, String url) { 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httpost = new HttpPost(url); 

    try { 
     httpost.setEntity(new ByteArrayEntity(jsonData.toString().getBytes("UTF8"))); 
     httpost.setHeader("Accept", "application/json"); 
     httpost.setHeader("Content-type", "application/json;charset=UTF-8"); 
     httpost.setHeader("Accept-Charset", "utf-8"); 
     HttpResponse httpResponse = httpclient.execute(httpost); 
     return httpResponse; 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; 
} 

然後在客戶端處理這樣的服務器響應:

String responseBody = EntityUtils 
        .toString(response.getEntity(), "UTF-8");