4

我正在尝试使用 Spring for Android 对 url 执行标准 HTTP POST,其中的主体只是参数列表(如键值对)而不是 JSON 对象。

我希望将响应从 JSON 转换为 Java ResponseObject,但据我所知,Spring 也将我的主体转换为 JSON。

这是我的代码:

Map<String, Object> params = new HashMap<String, Object>();
    params.put("client_id", mClientId);
    params.put("state", mState);
    params.put("username", mUsername);
    params.put("password", mPassword);
    return getRestTemplate().postForObject(url, params, ResponseObject.class);

先感谢您!

4

2 回答 2

4

试试这个

发布简单的名称值对列表

// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name", nameString));
nameValuePairs.add(new BasicNameValuePair("Country", "country name"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
}catch (ClientProtocolException e) {
 // TODO Auto-generated catch block
}
于 2013-03-05T11:27:09.603 回答
2

采用.exchange()

// Create the request body as a MultiValueMap
MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();     

body.add("client_id", mClientId); // and so on

// Note the body object as first parameter!
HttpEntity<?> httpEntity = new HttpEntity<Object>(body, requestHeaders);

MyModel model = restTemplate.exchange("/api/url", HttpMethod.POST, httpEntity, MyModel.class);
于 2013-01-15T04:54:48.990 回答