简体   繁体   中英

Connecting to Jersey REST Web Service from Android

I created simple REST Web Service which takes three Strings from client and saves them to the database. It's working fine from Java client application.

Now, I want to do the same from Android device. What would be the best way to connect to service from my device and do the same?

I am new to Android, searched a lot on web, just couldn't find something simple enough to understand how to do it. Hope somebody can help.

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

    }

}

I found solution for this, but most of the classes are deprecated. It's working fine (for now), will try to find better solution and post it here.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM