Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I send JSon as BODY In a POST request to server from an Android application?
    primarykey
    data
    text
    <p>I am working on my first Android Application. What I am trying to do is a POST request to a REST service I want the BODY of this request to be a JSON String. </p> <p>I am using google's GSON to generate the JSON that is sent to the server. Here is the code doing POST request: </p> <pre><code>HttpPost requisicao = new HttpPost(); requisicao.setURI(new URI(uri)); requisicao.setHeader("User-Agent", sUserAgent); requisicao.setHeader("Content-type", "application/json"); HttpResponse resposta = null; //I can see the json correctly print on log with the following entry. Log.d(TAG, "JSon String to send as body in this request ==&gt;&gt; " + jsonString); //than I try to send JSon using setEntityMethod StringEntity sEntity = new StringEntity(jsonString, "UTF-8"); requisicao.setEntity(sEntity); resposta = httpClient.execute(requisicao); resultado = HttpProxy.leRespostaServidor(resposta); </code></pre> <p>The response code is 400 BAD REQUEST and from the server log I can read the info. where it says the body was not correctly sent: </p> <pre><code>13:48:22,524 ERROR [SynchronousDispatcher] Failed executing POST /peso/cadastrar/maia.marcos@gmail.com org.jboss.resteasy.spi.BadRequestException: Could not find message body reader for type: class java.io.Reader of content type: application/json </code></pre> <p>The code for the server side is a simple Seam Rest Service: </p> <pre><code> @POST @Path("/cadastrar/{userEmail}") @Consumes(MediaType.APPLICATION_JSON) public String cadastraPeso(@PathParam("userEmail") String email, Reader jsonString) { LineNumberReader lnr = new LineNumberReader(jsonString); try { String json = lnr.readLine(); if(json != null) { log.debug("String json recebida do device ==&gt;&gt; " + json); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "OK - o e-mail processado foi ==&gt;&gt; " + email; } </code></pre> <p>What could be wrong with the Android client code? I have researched the web and did not find any really useful information about this error.</p> <p>[]s</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload