2014-10-07 106 views
0

我正在做Android中的REST客戶端以使用託管在Amazon EC2中的webService。Rest客戶端Android與亞馬遜EC2

當訪問bowser中的URL時,它工作正常。但是當我試圖接取web服務的Android應用程序,我收到了錯誤404

  URL urla = new URL(SERVICE_URL); 

      HttpGet method= new HttpGet(new URI(SERVICE_URL)); 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(); 
      request.setURI(new URI(SERVICE_URL)); 
      HttpResponse response = client.execute(method); 
      int responseCode = response.getStatusLine().getStatusCode(); 

當我提供規則「全部」訪問亞馬遜EC2,它的做工精細,但我相信這不是一個更好的方法。

所有規則:

*端口:所有

協議:所有

資料來源:*

有誰知道一個更好的方式來訪問託管的REST Web服務EC2與客戶端Android?

+0

你在responseCode中看到404了嗎?答案是正確的,或者是404頁面?我建議你使用這個庫項目來調用APIS。它使您可以輕鬆實現,清晰的代碼和許多可能性。我希望這會幫助你。 https://github.com/matessoftwaresolutions/AndroidHttpRestService – 2014-10-07 23:17:42

回答

0

嘗試使用AsyncHttpClient庫,而不是簡單和容易。下面是一個示例代碼

AsyncHttpClient client; 
client.get("http://ec2-xx-xxx-xx-xx.compute-1.amazonaws.com:8080/MyService1/jaxrs/helloworld", 
     new AsyncHttpResponseHandler() { 
     // When the response returned by REST has Http 
     // response code '200' 
     @Override 
     public void onSuccess(String response) { 
      try { 
       // JSON Object 
       tv1.setText(response); 
      } catch (Exception e) { 
       tv1.setText(e.getMessage()); 
      } 
     } 
    }); 

簡單給您的實例的公共DNS服務器使用的端口,然後路徑爲您服務的函數調用。 這就是我做到的。