2015-09-24 21 views
1

我的android servlet被設計爲在Apache Tomcat服務器上用tomcat servlet發佈請求並接收響應。爲了進行調試,我使用相同的POST和GET方法設置了Servlet,以便通過瀏覽器嘗試功能和可訪問性。Tomcat連接上的AVD出現奇怪的FileNotFoundException

簡而言之:當我部署應用程序時,我可以通過10.0.2.2:8080/my_app?request=test輕鬆地從AVD設備瀏覽器訪問它,並且我得到了一個很好的結果。 localhost:8080/my_app?request=test. 但是,當我從我的應用程序嘗試它時,我總是得到java.io.FileNotFoundException: http://10.0.2.2:8080/my_app。 爲什麼?

我到目前爲止嘗試了什麼:該應用程序具有互聯網權限,他們也工作,爲了到達Servlet通信點,我必須首先通過PHP進行登錄程序,並且它位於同一臺服務器上並運行一般。

AsyncTask連接到servlet看起來像這樣:

 AsyncTask<Void,Void,String> getDBdata = new AsyncTask<Void, Void, String>() { 
      @Override 
      protected String doInBackground(Void... params) { 
       URL url = null; 
       try { 
        url = new URL("http://10.0.2.2:8080/my_app"); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } 
       String text; 
       text = null; 
       JsonArray js = null; 
       try { 
        HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
        connection.setDoOutput(true); 
        connection.setRequestMethod("POST"); 
        connection.setRequestProperty("action", "getDBData"); 
        connection.setDoInput(true); 
        connection.connect(); 
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
        StringBuilder builder = new StringBuilder(); 
        String aux = ""; 

        while ((aux = in.readLine()) != null) { 
         builder.append(aux); 
        } 
        text = builder.toString(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       return text; 

回答

2

好吧,當然我一直想在頭髮送參數,可以和這導致BS。菜鳥錯誤!

bcody的回答來自this question幫助我調試了很多!另外,從servlet中查看服務器協議可能會讓我更早地發現錯誤。

這是最後的工作代碼:

AsyncTask<Void,Void,String> getDBdata = new AsyncTask<Void, Void, String>() { 
    @Override 
    protected String doInBackground(Void... params) { 
     URL url = null; 
     try { 
      url = new URL(Constants.SERVER_URL + getDBdataURL); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     String text; 
     text = null; 
     try { 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestProperty("Accept-Charset", "UTF-8"); 
      connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible) "); 
      connection.setRequestProperty("Accept", "*/*"); 
      connection.setChunkedStreamingMode(0); 
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      connection.setDoOutput(true); 
      connection.setRequestMethod("POST"); 
      String request = "action=getDBdata"; 
      PrintWriter pw = new PrintWriter(connection.getOutputStream()); 
      pw.print(request); 
      pw.close(); 
      BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      StringBuilder builder = new StringBuilder(); 
      String aux = ""; 
      while ((aux = in.readLine()) != null) { 
       builder.append(aux); 
      } 
      text = builder.toString(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return text; 
    }