如何发送JSON作为身在一个POST请求到服务器从Android应用程序? [英] How do I send JSon as BODY In a POST request to server from an Android application?

查看:177
本文介绍了如何发送JSON作为身在一个POST请求到服务器从Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作我的第一个Android应用程序。现在笏我想要做的就是让一个POST请求到服务器上运行REST风格的服务,我想这个请求的主体是一个JSON字符串。

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.

我使用GSON生成JSON发送给服务器。在code我使用,使POST请求如下:

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);

响应code是400错误的请求,并从服务器日志我可以读取信息。它说,身体不正确发送:

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/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服务器端是一个简单的缝REST服务:

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;
 }

这可能是错误的Andr​​oid客户端code?我已经研究了网上,并没有发现任何关于这一问题真正有用的信息。

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

推荐答案

对不起乡亲,只是原来的错误是在休息的服务。我不得不改变它接受一个字符串,而不是读者对象,而现在它的作品如预期,在服务器端的休息code现在是:

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;
}

和JSON字符串被正确地接收服务器端。

And the JSon string is correctly receive in server side.

所以去的Andr​​oid code以上它按预期工作。

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

问候。

这篇关于如何发送JSON作为身在一个POST请求到服务器从Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆