Skip to content

Instantly share code, notes, and snippets.

@rciovati
Last active August 9, 2018 17:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rciovati/4125119 to your computer and use it in GitHub Desktop.
Save rciovati/4125119 to your computer and use it in GitHub Desktop.
POST request with robospice
import com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public class MyRequest extends SpringAndroidSpiceRequest<MyResponse> {
public MyRequest(){
super(MyResponse.class);
}
@Override
public MyResponse loadDataFromNetwork() throws Exception {
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
parameters.set("foo", "bar");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(parameters, headers);
return getRestTemplate().postForObject("http://www.yoursite.com", request, MyResponse.class);
}
}
@Wicowyn
Copy link

Wicowyn commented Aug 5, 2013

It can help some people, I had to add this line to it working :
"getRestTemplate().getMessageConverters().add(new FormHttpMessageConverter());"

@hoseinit
Copy link

Still not working for me
An exception occurred during request network execution :null

@lcofre
Copy link

lcofre commented Nov 2, 2016

@hoseinit
I removed the headers.setContentType() line and it worked.

As stated here, the Spring RestTemplate will figure out the content type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment