【问题标题】:How to send JSON data as parameter from Android to Jersey RESTful Service如何将 JSON 数据作为参数从 Android 发送到 Jersey RESTful 服务
【发布时间】:2013-05-16 16:41:48
【问题描述】:

我正在为我的 Android 应用程序创建一个登录功能,该应用程序将连接到 Jersey RESTful Web 服务。作为服务调用的一部分,我将发送一个 JSON 对象,但我现在的问题是我不知道如何在 Web 服务端解密 JSON。这是我的代码:

Android(AsyncTask - doInBackground):

try {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost post = new HttpPost(
            "http://X.X.X.X:8080/HearIt/services/AuthMySQL");
    post.setHeader("content-type", "application/json");

    JSONObject dato = new JSONObject();
    dato.put("email", params[0]);
    dato.put("password", Object_Cipher.init(params[1]));

    StringEntity entity = new StringEntity(dato.toString());
    post.setEntity(entity);
    HttpResponse resp = httpClient.execute(post);
    return EntityUtils.toString(resp.getEntity());

} catch (Exception E) {
    E.printStackTrace();
}

网络服务:

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })   
public String AuthMySQL("WHAT I NEED PUT HERE") {
    return "none";
}

我需要做什么才能获取 JSON 数据?谢谢。

【问题讨论】:

    标签: android json web-services rest post


    【解决方案1】:

    我建议阅读 JAX-RS 中的 Request and Response entities。您还应该阅读JAX-RS specification 的第 3.3 节,因为它涵盖了如何处理 JAX-RS 中的参数的(非常)技术方面。在最简单的情况下,您可以简单地为您的资源方法提供一个字符串参数,然后将传入的数据 (JSON) 存储到其中:

    @POST
    @Produces({ MediaType.APPLICATION_JSON })
    @Consumes({ MediaType.APPLICATION_JSON })   
    public String AuthMySQL(String json) {
        System.out.println("The JAX-RS runtime automatically stored my JSON request data: " + json);
    }
    

    当然,这并没有利用 JAX-RS 可以提供的自动映射。正确设置后,运行时实际上可以将传入的数据直接反序列化为 Java 类(假设您已经创建了一个)。因此,例如,给定以下类:

    class LoginData {
        private String email;
        private String password;
    
        // constructors, getters/setters
    }
    

    您可以选择将请求数据直接编组到其中:

    @POST
    @Produces({ MediaType.APPLICATION_JSON })
    @Consumes({ MediaType.APPLICATION_JSON })   
    public String AuthMySQL(LoginData data) {
        System.out.println("The JAX-RS runtime automatically deserialized my JSON request data: " + data);
    }
    

    要成功完成此操作,您需要在应用程序中包含 jersey-json 模块。

    【讨论】:

      【解决方案2】:

      我会尝试做一个通用的例子供你学习。

      假设您要使用以下 JSON:

      { "user": {
          "name": "john",
          "age": 20,
          "country": "austria"
          }
      }
      

      您必须创建以下类:

      class User {
          String name;
          Integer age;
          String country;
      
          // getters, setters, whatevers
      }
      

      在网络服务中:

      @POST
      @Produces({ MediaType.APPLICATION_JSON })
      @Consumes({ MediaType.APPLICATION_JSON })   
      public String consumeUserName(User user) {
          return user.getName();
      }
      

      【讨论】:

      • 如何使用 HTTP POST 传递 List
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多