2016-09-14 20 views
0

我正在爲我的android應用創建登錄GUI,其中我是試圖通過使用HTTP請求向我的android應用獲取JSON數據 'GET'(用於用戶名+密碼)。問題是我收到HTML數據,這讓我瘋狂。我有一個理論,也許登錄功能設置了一些cookie(例如,JSESSIONID),並且他們沒有被保存(因此無法登錄),但我不確定。我錯過了在代碼中指定的東西嗎?在使用HTTP'GET'作爲請求方法時獲取HTML而不是JSON數據

另外,當我在logcat中打印cookies變量時,我只會得到一堆空值。

代碼:

private class DownloadStartTilesData extends AsyncTask<String, Void, String> { 

    String jsonResponse = ""; 
    String urlRequest; 

    private List<String> cookies; 
    static final String COOKIES_HEADER = "Set-Cookie"; 

    @Override 
    protected String doInBackground(String... params) { 

     // Log.d("JSON", "Json: " + urlRequest); 

     URL url; 
     HttpURLConnection client; 

     try { 

      CookieManager cookieManager = new CookieManager(); 
      CookieHandler.setDefault(cookieManager); 

      url = new URL(urlRequest); 

      client = (HttpURLConnection) url.openConnection(); 
      client.setRequestMethod("GET"); 
      client.setRequestProperty("Connection", "Keep-Alive"); 

      // Cookie stuff: 
      if (cookies != null) { 
       for (String cookie : this.cookies) { 
        client.addRequestProperty("Cookie", cookie.split(";", 2)[0]); 
       } 
      } 

     (iPhone)"); 
      client.setRequestProperty("Content-Type", "Logon/json"); 
      client.setDoOutput(true); 
      client.setDoInput(true); 


      Log.d("Response:", "" + client.getResponseCode()); 
      BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()), 8192); 
      String inputLine; 
      while ((inputLine = in.readLine()) != null) { 
       jsonResponse = jsonResponse.concat(inputLine); 
       System.out.println(inputLine); 
      } 


      in.close(); 

      setCookies(client.getHeaderFields().get("Set-Cookie")); 


      // out.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return ""; 
    } 

    @Override 
    protected void onPostExecute(String result) { 

    } 

    @Override 
    protected void onPreExecute() { 
     BackendURLs backendURLs = new BackendURLs(BuildConfig.SERVER_URL); 


    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
    } 

    // More cookies stuff: 
    public void setCookies(List<String> cookies) { 
     this.cookies = cookies; 
    } 
} 

}

的jsonResponse實際上給HTML。任何幫助或指出解決我的問題將不勝感激。

HTML響應:

<!DOCTYPE html> 
<html lang="sv" xml:lang="sv"> 
    <head> 
     <title>Generic Application Error Test JSP (Item)</title> 
     <link rel="stylesheet" href="/wcsstore/SharedStorefrontAssetStore/include/styles/style1/css/desktop.css" type="text/css" media="screen"/> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> 
    </head> 
    <body> 

     <div class="page"> 
      <header> 
       <div class="line size1of1"> 
        <div class="unit size1of3"> 
         <ul class="mod fz_12px border-bottom_3px header-list-top bold"> 
          <li class="not-active-store"> 
           <a 
          <li class="not-active-store"> 
         </ul> 
        </div> 
        <div class="unit size2of3"> 
         <ul class="mod fz_12px border-bottom_3px header-list-top bold border-left"> 
          <li class="not-active-store"> 
           &nbsp;Application Error 
          </li> 
         </ul> 
        </div> 
       </div> 
       <div class="line major-header-line border-bottom_3px"> 
        <div class="unit size2of3"> 
         <div class="logo-search-field"> 
          <img class="logo-header" src="/wcsstore/SharedStorefrontAssetStore/include/styles/style1/images/logo.png"/> 
         </div> 
        </div> 
       </div> 
      </header> 
      <section class="mainWrap"> 
       <article class="line"> 
        <div class="margin-top_20px margin-bottom_20px"> 
         <h2>Ett fel har intr&auml;ffat</h2> 
         <p>Det verkar som n&aring;got har g&aring;tt lite galet p&aring; webbsidan. Visa de tekniska detaljerna nedan, kontakta sedan kundtj&auml;nst om du inte kan l&ouml;sa felet sj&auml;lv.</p> 
         <p><a href="#" onclick="jQuery('#errorDetails').show();">Visa tekniska detaljer</a></p> 
         <div class="details" id="errorDetails" style="display: none"> 

          <p><strong>TYP</strong>: 0</p> 
          <p><strong>KEY</strong>: _ERR_CMD_CMD_NOT_FOUND</p> 
          <p><strong>MSG</strong>: CMN3101E The system is unavailable due to &#034;_&#034;.</p> 
          <p><strong>SYS</strong>: Command not found: &#034;_&#034;.</p> 
          <p><strong>ORG</strong>: </p> 

         </div> 
        </div> 
       </article> 
      </section> 

      <footer class="box bottom" style="padding-bottom: 4px;"> 
      </footer> 
     </div> 
    </body> 
</html> 
+1

我們應該怎麼知道?我們不知道這個網站預期或需要輸入。如果此代碼執行的操作與您在瀏覽器中獲得的內容不同,那麼您需要弄清楚其中的差異,然後複製它。 –

+0

發佈您的HTML回覆。 – Isuru

+0

我已經發布了。 – Jazzbaron

回答

0

顯然,問題與cookies奠定正如我前面所懷疑。當發生重定向時,Cookie(其登錄名和密碼ID所在的位置)丟失,因此JSON數據請求在沒有任何登錄憑證的情況下被處理(因此錯誤的HTML消息)。

通過使用如下代碼手動停止第一重定向:

connection.setInstanceFollowRedirects(false); 

我能夠連接前一次保存的cookies更多:

String cookies = connection.getHeaderFields().get("Set-Cookie").toString().replaceAll("\\[|\\]", ""); 

    URL url2 = new URL(connection.getHeaderField("Location")); 
    HttpURLConnection connection2 = (HttpURLConnection) url2.openConnection(); 
    connection2.setRequestProperty("Cookie", cookies); 

我不知道爲什麼會重定向吃掉所有的餅乾,但是,你有它們。

相關問題