2015-03-31 153 views
1

我創建了簡單的REST Web服務,它從客戶端獲取三個字符串並將它們保存到數據庫。它從Java客戶端應用程序運行良好。從Android連接到Jersey REST Web服務

現在,我想從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(); 

    } 

} 
+0

額外的罐子會增加應用程序的大小,您可以使用Java HTTP URL連接類 – 2015-03-31 08:17:35

+0

額外的罐子一樣,做同樣喜歡我在Java應用程序那樣輕鬆地調用REST服務?試過了,事情變得非常複雜,我放棄了,我正在尋找更好的解決方案。 將嘗試與HTTP URL連接,是的,只是爲了弄清楚如何做到這一點。 – Tinmar 2015-03-31 08:23:29

+1

Android SDK沒有JDK中提供的所有類,因此您不能指望第三方jar在android上工作,除非它們不兼容,因此我建議使用Android SDK中提供的標準類,而不是使用Java應用程序罐。 HttpURLConnection是非常comman類和數以千計的網上可用的例子。 – 2015-03-31 09:03:08

回答

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