title: Spring (for Android) and Jackson2: PUT as application/x-www-form-urlencoded
body: <p>I am somewhat new to HTTP REST operations on Android, and a server I am working with uses PUT commands to process updates. I am having a difficult time using Spring (for Android) with Jackson2. The server doesn't seem to work with <code>application/json</code> put requests (though it will reply with them), and only seem to work with <code>application/x-www-form-urlencoded</code> versions (tested with python and curl. On python, if I set the header type to <code>application/json</code>, it fails.</p>
  
  <p>I am using the latest versions of Spring and Jackson2, and I know everything is setup properly because my get request on the same URL gets me all the correct information.</p>
  
  <p>I am using Robospice, but I don't really think that is relevant. Here is my request code.</p>
  
  <pre><code>@Override
  public GPIO loadDataFromNetwork() throws Exception {
      String url = String.format("http://%s/api/control/gpio", ip);
      RestTemplate rt = getRestTemplate();
      DefaultHttpClient client = new DefaultHttpClient();
      Credentials defaultcreds = new UsernamePasswordCredentials("admin",
              password);
  
      client.getCredentialsProvider().setCredentials(
              new AuthScope(routerip, 80, AuthScope.ANY_REALM), defaultcreds);
      // Makes authentication work.
      rt.setRequestFactory(new HttpComponentsClientHttpRequestFactory(client));
      HttpHeaders requestHeaders = new HttpHeaders(); 
      requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  
      HttpEntity request = new HttpEntity(data, requestHeaders);
      List&lt;HttpMessageConverter&lt;?&gt;&gt; messageConverters = new ArrayList&lt;HttpMessageConverter&lt;?&gt;&gt;();
      MappingJackson2HttpMessageConverter map = new MappingJackson2HttpMessageConverter();
      messageConverters.add(map);
      messageConverters.add(new FormHttpMessageConverter());
      rt.setMessageConverters(messageConverters);
  
      ResponseEntity&lt;GPIO&gt; r = rt.exchange(url, HttpMethod.PUT, request, GPIO.class);
      return r.getBody();
  }
  </code></pre>
  
  <p>I am getting the exception stating it cannot find a way to convert:</p>
  
  <pre><code>02-01 10:59:29.466: E//DefaultRequestRunner.java:138(30086): 
  10:59:29.474 Thread-11651 An exception occurred during request network 
  execution :Could not write request: no suitable HttpMessageConverter 
  found for request type [com.xxxxx.control.gpio.GPIO] and content 
  type [application/x-www-form-urlencoded]
  </code></pre>
  
  <p>GPIO is my POJO object. I want to 'put' that to the server, as in serialize and put it.</p>
  
  <p>I have looked at the following question that seems fairly relevant:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/14491309/deserializing-nested-objects-using-resttemplate?rq=1">Deserializing Nested objects using RestTemplate</a></li>
  </ul>
  
  <p>However, I need the result of my put command, and that requires me to use <code>exchange()</code> because Spring's <code>put()</code> returns nothing.</p>
  
  <p>I have tried several different items (such as removing GPIO references, setting specific headres...) and none seem to work. I have a feeling this is probably an easy solution that I don't know how to fix. If anyone can help me that would be great.</p>
  
  <p>TLDR: I'm using Spring for Android with Jackson2. I want to serialize my object (in this example, GPIO) so I can do a PUT request with the content type application/x-www-form-urlencoded. However I cannot get jackson to convert to that type, only to application/json, which does not work for me. I am not sure how to fix this, and I have run out of ideas. If I can't find a solution I'll probably have to dumb robospice. (or jackson, not sure which yet.)</p>
  
  <p><strong>Solution</strong></p>
  
  <p>Spring for Android doesn't seem to simplify things, so I dumped it and used the apache client directly in my loadDataFromNetwork() method. Robospice handles it pretty well and I can get the responses I need. If you are new to HTTP like I was take the time and learn the apache client, it's far easier in my opinion. Tweaking the ObjectMapper (like making a JsonTree and parsing that) made it much easier to get the data I needed without having to do as much work with POJO objects.</p>