簡體   English   中英

HttpResponse在Android和Java中有所不同

[英]HttpResponse differs in Android and Java

我正在尋找幫助! 我有一些執行服務器身份驗證並從服務器檢索響應的代碼。 當我在Java項目的eclipse中執行它時,我得到了reddirrect響應,例如:

<html><head><meta http-equiv="refresh" content="0;url=SiteIamLoggingPageURL/"/></head></html>

但是,當我在我的android應用中執行相同的代碼時,我得到的響應等於登錄失敗的情況。

HttpPost post = new HttpPost(loginPageAdress);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//nameValuePairs.add(new BasicNameValuePair("remember", "1"));
nameValuePairs
        .add(new BasicNameValuePair("email", "email"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));

post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
StringBuilder stringBuilder = new StringBuilder();

HttpResponse response = client.execute(post, httpContext);
BufferedReader rd = new BufferedReader(new InputStreamReader(response
        .getEntity().getContent(), "UTF-8"));
String line = "";
while ((line = rd.readLine()) != null) {
    stringBuilder.append(line).append("\n");
}
System.out.println(stringBuilder.toString());

問題是什么? oo謝謝您的時間!

聽起來好像您忘了添加

 <uses-permission android:name="android.permission.INTERNET" />

在AndroidManifest.xml中。 看看這個教程

  1. 如果這是您的身份驗證代碼,則必須通過標題而不是通過HTML刷新元標記使用重定向。 服務器應發出位置:http:/authenticated.jsp fe
  2. 要檢查實際情況,請使用Wireshark。 那你就不再瞎了。 此處-來自Android的請求,此處來自服務器的答案。 這是一個非常有用的工具,花一些時間來處理它。

可能是因為您沒有在單獨的Thread上執行HTTP請求。 在這里閱讀有關此內容的更多信息:

為什么冰淇淋三明治會崩潰您的應用程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM