如何将JSon作为BODY发送到Android应用程序的服务器POST请求中?

时间:2022-12-01 16:26:17

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.

我正在开发我的第一个Android应用程序。我想要做的是对REST服务的POST请求我希望此请求的BODY是JSON字符串。

I am using google's GSON to generate the JSON that is sent to the server. Here is the code doing POST request:

我正在使用谷歌的GSON生成发送到服务器的JSON。这是执行POST请求的代码:

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:

响应代码是400 BAD REQUEST,从服务器日志中我可以读取信息。它说身体没有正确发送:

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

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

服务器端的代码是一个简单的Seam Rest服务:

    @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 about this error.

Android客户端代码有什么问题?我研究过网络,但没有找到任何有关此错误的有用信息。

[]s

[]中

1 个解决方案

#1


11  

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

对不起,大家发现错误发生在Rest服务上。我已经更改了它,现在它接收到一个String而不是Reader对象,它按预期工作,服务器端的REST端点代码现在是:

@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 received from device ==>> " + json);
        }   
        return "OK - processed email ==>> " + email;
}

And the JSON string is correctly received on server side.

并且在服务器端正确接收JSON字符串。

So de Android code above is working as expected.

所以上面的Android代码正如预期的那样工作。

#1


11  

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

对不起,大家发现错误发生在Rest服务上。我已经更改了它,现在它接收到一个String而不是Reader对象,它按预期工作,服务器端的REST端点代码现在是:

@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 received from device ==>> " + json);
        }   
        return "OK - processed email ==>> " + email;
}

And the JSON string is correctly received on server side.

并且在服务器端正确接收JSON字符串。

So de Android code above is working as expected.

所以上面的Android代码正如预期的那样工作。