问题
我正在考虑将我现有的 http post 方法转换为spring-android
但我失败了。
JSONObject defaultJsonObject = new JSONObject();
defaultJsonObject.put("ln", "Kiat");
defaultJsonObject.put("CountryName", "Malaysia");
defaultJsonObject.put("CityName", "Kuala Lumpur");
这是我现有的 http 帖子,它正在运行,并将形成一个帖子正文:[json={"ln":"Kiat","CountryName":"Malaysia","CityName":"Kuala Lumpur"}]
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("json", jsonObject.toString()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
System.out.println("post param: " + postParams.toString());
post.setEntity(entity);
post.setHeader("Accept", "application/json");
但是当我使用 RestTemplate 转换为 Spring-android 时它失败了。即使我已经设法形成帖子正文,因为[json={"ln":"Kiat","CountryName":"Malaysia","CityName":"Kuala Lumpur"}]
我不断收到 500 Internal Server Error
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
List<NameValuePair> postParams1 = new ArrayList<NameValuePair>();
postParams1.add(new BasicNameValuePair("json", jsonObject.toString()));
HttpEntity<?> requestEntity = new HttpEntity<Object>(postParams1, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,String.class)
帖子正文格式将是这样的
[json={"ln":"Kiat","CountryName":"Malaysia","CityName":"Kuala Lumpur"}]