【问题标题】:Receive JSON Variables using Jersey sent from and Android device using HttpPost使用从 HttpPost 和 Android 设备发送的 Jersey 接收 JSON 变量
【发布时间】:2013-02-19 18:57:40
【问题描述】:

我有一个使用 HttpPost 发送数据的 Android 代码:

public static void sendRideIdAndEmail(String rideId,String email){
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
    HttpResponse response;
    JSONObject json = new JSONObject();

    try {
        HttpPost post = new HttpPost("http://192.168.1.59/FareShareREST/api/rides/joinRide");
        json.put("rideid", rideId);
        json.put("emailid", email);
        Log.v("RideEmailJson",json.toString());
        StringEntity se = new StringEntity( json.toString());  
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(se);
        response = client.execute(post);

        /*Checking response */
        if(response!=null){
            InputStream in = response.getEntity().getContent(); //Get the data in the entity
        }

    } catch(Exception e) {
        e.printStackTrace();
        Log.v("Error", "Cannot Estabilish Connection");
    }

}

我在 Tomcat 7 上运行的服务器端有一个 RestFul Post 方法:

   @POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("joinRide")
public Response joinRide(@PathParam("rideid") long rideid,@PathParam("emailid") String     emailid) {
    System.out.println(emailid);
    Ride ride=rideRepository.getRideById(rideid);
    UserDetail userDetail=userRepository.getUserDetailbyEmailId(emailid);
    User user=new User();
    user.setName(userDetail.getName());
    user.setRegistrationId(userDetail.getRegistrationId());
    rideRepository.addUserToRide(ride, user);
    return Response.ok().build();
}

但我总是收到电子邮件 ID 为 null。我尝试使用 @FormParam。但这似乎行不通。

有人可以帮我吗?

【问题讨论】:

    标签: java android rest jersey


    【解决方案1】:

    路径参数 (@PathParam) 是从传入的请求 URL 中提取的。因此,例如,使用 URL http://192.168.1.59/FareShareREST/api/rides/joinRide/5 和以下代码:

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/joinRide/{rideid}")
    public Response joinRide(@PathParam("rideid") long rideid) {
        System.out.println(rideid);
    }
    

    你会得到rideid 映射到55 的值来自被映射的 URL 部分 (/joinRide/{id})。 但是,这对您的场景没有用 - 您正在创建一个 JSON 对象并将其包含在请求正文中。在这种情况下,在服务器端,JAX-RS 提供者会将此请求主体映射到资源方法上的第一个合格参数。你想要类似这样的代码:

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/joinRide")
    public Response joinRide(String json) {
        System.out.println(json);
    }
    

    这会将您的 JSON 字符串直接设置到名为 json 的变量中。当然,您需要解析 JSON 以获取其中的数据,而您可能不想这样做。在这种情况下,创建一个包含预期数据字段的简单 POJO,并在资源方法中使用它:

    public class EmailInfo implements Serializable {
        private String rideId;
        private String email;
    
        // Constructors, Getters/Setters
    }
    
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/joinRide")
    public Response joinRide(EmailInfo info) {
        System.out.println(info.getRideId());
        System.out.println(info.getEmail());
    }
    

    【讨论】:

    • 谢谢。但我只想在 POST 中传递可以在服务器端接收的变量。我有什么办法
    • 好的,所以你的服务器端是固定的,但是你可以改变客户端吗?
    • 我只想从我的 android 应用程序中发布变量。这样就可以在Server上接收了
    • 对不起,我没有关注你。如果您无法更改服务器端,则需要将您尝试传递的那些变量附加到 URL(例如:192.168.1.59/FareShareREST/api/rides/joinRide/5/bob@foo.com)。请注意,由于其中包含电子邮件地址,您必须对 URL 进行编码。另一方面,如果您可以更改服务器端,请查看我的答案并修改您的代码以匹配。
    • 好的。然后我会更喜欢制作一个像 EmailInfo 这样的包装类并做到这一点。谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多