2015-12-02 79 views
0

我正在捕捉一個設備事件,並根據來自它的信息我需要進行登錄。如果登錄已驗證,我想轉到我的應用程序的下一頁。執行一個servlet,以便我的doGet方法被調用

我試着用HttpURLConnection來做,但它並沒有真正執行servlet。

這是我使用的遺傳密碼:

public static String getResponseFromUrl(String url) throws Exception { 
    URL website = new URL(url); 

    HttpURLConnection connection = (HttpURLConnection)website.openConnection(); 
    connection.setRequestMethod("GET"); 
    connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); 
    connection.setUseCaches(false); 

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
    String response = ""; 
    String inputLine; 

    while ((inputLine = in.readLine()) != null) 
     response += inputLine; 

    in.close(); 

    return response.toString(); 
} 

我也試過

InputStream responseInputStream = connection.getInputStream(); 

但具有相同的結果。 doGet方法沒有被調用,顯然該應用程序不會移動到下一頁。

我很確定這可以通過在JSP頁面內捕獲事件來完成,但我希望有一個更簡單的解決方案。

編輯:當在瀏覽器中提交相同的URL它一切正常。 doGet被調用並且應用程序移動到下一頁。

+0

doGet()not called mean doGet()not invoked或getResponseFromUrl()not invoke?或其他東西 –

+0

getResponseFromUrl()被調用並且沒有錯誤地運行。但doGet沒有被調用。 – Eddy

+0

這意味着在當時獲取請求時doGet()沒有被調用正確的時候開始?如果doGet not invoke意味着所有其他bla bla不執行。我正確的想法? –

回答

-1

從DOC:

URL url = new URL("http://www.android.com/"); 
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
    try { 
    InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
    readStream(in); 
    finally { 
    urlConnection.disconnect(); 
    } 
} 

與試 「readStream(在);」

如果它仍然無效,錯誤必須來自URL。 你說你有這個URL的doGet方法,請嘗試使用你的瀏覽器來確保URL響應。你真的需要一個不同的doGet/doPost嗎?使用「服務」方法代替

+0

你有沒有試過這段代碼?也許doGet方法被調用,但不移動到下一頁 – MeGoodGuy

相關問題