简体   繁体   中英

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 :

<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.

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!

Sound like you forgot adding

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

in 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. Server should issue Location: http:/authenticated.jsp fe
  2. To check whats really going on use Wireshark. Then you not blind anymore. Here - request from Android here answer from server. It is very useful tool spend some time with it.

It could be because your not executing the HTTP request on a separate Thread . Read more about this here:

Why Ice Cream Sandwich Crashes Your App

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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