Question

I need to send a POST HTTP request using Robospice for Android that will have data in the following format:

Content-Type: application/json

{'var1':'var1val','telNumber':'var2val'}

There may be other HTTP headers inside but this is the only header required by the server - I've tested it with a simple HelloWorld request that doesn't require any specific objects in the body.

The problem is that I can't send request body in the right format. I'm doing everything according the Robospice tutorial.

loadDataFromNetwork() method

public T loadDataFromNetwork() throws Exception { // I'm making generic requests, T is a class representing given request
    Uri.Builder uriBuilder = Uri.parse(mUrl).buildUpon(); // mURL is just a full URL, including the method (just HTTP, not HTTPS)
    MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();
    for (NameValuePair nvp : mPostParams) { // From the constructor: mPostParams = new ArrayList<NameValuePair>();
        body.add(nvp.getName(), nvp.getValue());
    }

    HttpEntity<?> requestEntity = new HttpEntity<Object>(body, mHttpHeaders);
    RestTemplate restTemplate = new RestTemplate();

    ResponseEntity<T> responseEntity = getRestTemplate().exchange(
            uriBuilder.build().toString(), HttpMethod.POST, requestEntity,
            mClazz);
    return responseEntity.getBody();
}

createRestTemplate() in the JsonSpiceService class (which extends SpringAndroidSpiceService)

public RestTemplate createRestTemplate() {
    RestTemplate restTemplate = new RestTemplate(true);

    MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
    FormHttpMessageConverter formHttpMessageConverter = new
    FormHttpMessageConverter();
    StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
    final List<HttpMessageConverter<?>> listHttpMessageConverters = restTemplate
            .getMessageConverters();

    setTimeout(restTemplate);

    listHttpMessageConverters.add(jsonConverter);
    listHttpMessageConverters.add(formHttpMessageConverter);
    listHttpMessageConverters.add(stringHttpMessageConverter);
    restTemplate.setMessageConverters(listHttpMessageConverters);
    return restTemplate;
}

Doing it this way, I keep getting 500 Internal Server Error and I am pretty sure it is related to format of the body. Is there any way of getting a raw HTTP request (not the toString() version of some methods just a raw request)? What else can I be doing wrong - can it be about encoding of the body?

Note that I was testing the very same request with both Chrome app for sending REST requests and curl (worked fine).

Était-ce utile?

La solution

You are getting confused in your SpringAndroid usage. Look at the docs of HttpEntity, you will see that you use a MultiValueMap as the Body. In that case, you are right, body.toString is called.

MultiValueMap is used in the way you think it is only for headers. For the body, HttpEntity expects a Pojo basically, and thus considers you MultiValueMap as a Pojo.

A simple workaround would be to create by hand the string you want to post as a body :

String body = "key=value&key=value&key=value"

This argument could be used as the first parameter of the constructor of HttpEntity.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top