简体   繁体   English

HttpResponse在Android和Java中有所不同

[英]HttpResponse differs in Android and Java

I am looking for some help! 我正在寻找帮助! I have some code doing server auth and retrieving the response from server. 我有一些执行服务器身份验证并从服务器检索响应的代码。 When i execute it in eclipse in my java project I've got reddirrect response like : 当我在Java项目的eclipse中执行它时,我得到了reddirrect响应,例如:

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

But when i am excecuting the same code in my android app, ive got response equal to the failed login scenario. 但是,当我在我的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());

What is the problem? 问题是什么? oo Thank you for your time! oo谢谢您的时间!

Sound like you forgot adding 听起来好像您忘了添加

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

in AndroidManifest.xml. 在AndroidManifest.xml中。 Have a look at this tutorial 看看这个教程

  1. if this is your Authentication code you must use Redirection via Headers not via HTML refresh metatag. 如果这是您的身份验证代码,则必须通过标题而不是通过HTML刷新元标记使用重定向。 Server should issue Location: http:/authenticated.jsp fe 服务器应发出位置:http:/authenticated.jsp fe
  2. To check whats really going on use Wireshark. 要检查实际情况,请使用Wireshark。 Then you not blind anymore. 那你就不再瞎了。 Here - request from Android here answer from server. 此处-来自Android的请求,此处来自服务器的答案。 It is very useful tool spend some time with it. 这是一个非常有用的工具,花一些时间来处理它。

It could be because your not executing the HTTP request on a separate Thread . 可能是因为您没有在单独的Thread上执行HTTP请求。 Read more about this here: 在这里阅读有关此内容的更多信息:

Why Ice Cream Sandwich Crashes Your App 为什么冰淇淋三明治会崩溃您的应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM