从Android连接到Jersey REST Web服务

时间:2015-03-31 08:15:45

标签: java android json rest jersey

我创建了简单的REST Web服务,它从客户端获取三个字符串并将它们保存到数据库中。它可以从Java客户端应用程序中正常工作。

现在,我想在Android设备上做同样的事情。从我的设备连接到服务并执行相同操作的最佳方法是什么?

我是Android的新手,在网上搜索了很多,但是找不到足够简单的东西来理解如何操作。希望有人可以提供帮助。

RESTLocationService.java:

    @Path("/data")
public class RESTLocationService {

    @POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public Response createDataInJSON(JSONObject data) throws JSONException {

    String lon = data.getString("lon");
    String lat = data.getString("lat");
    String dateTime = data.getString("dateTime");

    String location = "Longitude: " + lon + ", latitude: " + lat
            + ", time: " + dateTime + ".";

    DataHiber dataHiber = new DataHiber(lon, lat, dateTime);
    DBUtils.saveLocations(dataHiber);

    return Response.status(201).entity(location).build();
    }
}

RESTLocationClient.java:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {

        Client client = Client.create();

        WebResource webResource = client
                .resource("http://192.168.111.77:8080/TebLocationService/rest/data/post");

        String longy = "46.8888778877";
        String latty = "12.9485495894859";
        String timey = "29/03/2015 14:03";

        JSONObject obj = new JSONObject();
        obj.put("lon", longy);
        obj.put("lat", latty);
        obj.put("dateTime", timey);

        ClientResponse response = webResource.type("application/json")
                .post(ClientResponse.class, obj);

        if (response.getStatus() != 201) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }

        System.out.println("Output from Server .... \n");
        String output = response.getEntity(String.class);
        System.out.println(output);

    } catch (Exception e) {

        e.printStackTrace();

    }

}

1 个答案:

答案 0 :(得分:0)

我为此找到了解决方案,但大多数类都已弃用。它工作正常(目前),将尝试找到更好的解决方案并在此处发布。

            Looper.prepare();
            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(),
                    10000);
            HttpResponse response;
            JSONObject obj = new JSONObject();

            String timey = DateFormat.getDateTimeInstance().format(new Date());

            try {
                HttpPost post = new HttpPost(SERVICE_URL);
                obj.put("lon", longitude);
                obj.put("lat", latitude);
                obj.put("dateTime", timey);
                StringEntity se = new StringEntity(obj.toString());
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                post.setEntity(se);
                response = client.execute(post);

                if (response != null) {
                    InputStream in = response.getEntity().getContent();
                }

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

            }

            Looper.loop();