views:

79

answers:

1

Hi,

I am working on my first Android Application. Now wat I want to do is to make a POST request to a restfull service running on server and I want the BODY of this request to be a JSon String.

I am using GSon to generate the JSon to send to server. The code I am using to make the POST request follows:

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 ==>> " + 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);

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:

13:48:22,524 ERROR [SynchronousDispatcher] Failed executing POST /peso/cadastrar/[email protected]
org.jboss.resteasy.spi.BadRequestException: Could not find message body reader for type: class java.io.Reader of content type: application/json

The code for the server side is a simple Seam Rest Service:

    @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 ==>> " + json);
   } 
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
   return "OK - o e-mail processado foi ==>> " + email;
 }

What could be wrong with the Android client code? I have researched the web and did not find any really useful information on this subject.

[]s

+1  A: 

Sorry folks, just turned out that the error was on the Rest service. I have change it to receive a String instead of the Reader object and now it works as expected, the Rest code on the server side now is:

    @POST
@Path("/cadastrar/{userEmail}")
@Consumes(MediaType.APPLICATION_JSON)
public String cadastraPeso(@PathParam("userEmail") String email, String jsonString)
{
        String json = jsonString;
        if(json != null)
        {
            log.debug("String json recebida do device ==>> " + json);
        }   
        return "OK - o e-mail processado foi ==>> " + email;
}

And the JSon string is correctly receive in server side.

So de Android code above it's working as expected.

regards.

Marcos Maia
@Marcos Maia Good Marcos. Two days from now, mark your own answer as accepted
Arthur Ronald F D Garcia
Hi Arthur, I have to tell you I am still a bit lost using stack overflow. As a starter it looks too much information for me, I mean, all the mecanics on this points, comments, posts, as a first impression looks a bit confuse these forums, but I have found great information using it so I hope to get used quick.
Marcos Maia
@Marcos I'm glad you find this site useful, but please accept your own answer as an accepted answer by clicking the icon below the down arrow. It will be highlighted as green when your mouse is over it.
Shervin
@Shervin It looks like I have to wait a couple of days to be able to do that. Ill do it than.
Marcos Maia