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">
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äffat</h2>
<p>Det verkar som något har gått lite galet på webbsidan. Visa de tekniska detaljerna nedan, kontakta sedan kundtjänst om du inte kan lösa felet sjä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 "_".</p>
<p><strong>SYS</strong>: Command not found: "_".</p>
<p><strong>ORG</strong>: </p>
</div>
</div>
</article>
</section>
<footer class="box bottom" style="padding-bottom: 4px;">
</footer>
</div>
</body>
</html>
我們應該怎麼知道?我們不知道這個網站預期或需要輸入。如果此代碼執行的操作與您在瀏覽器中獲得的內容不同,那麼您需要弄清楚其中的差異,然後複製它。 –
發佈您的HTML回覆。 – Isuru
我已經發布了。 – Jazzbaron