4

Spring for Android を使用して、JSON オブジェクトではなく、本体がパラメーターのリスト (キーと値のペアなど) である URL に対して標準の HTTP POST を実行しようとしています。

応答を 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 に答える