登录后应用不会重定向到下一页

时间:2013-04-15 14:07:51

标签: java android

我在我的应用程序中创建了登录页面,但是当用户输入正确的用户并且在屏幕上显示User Found但是应用程序没有重定向到下一个布局时,我遇到了问题。

PHP代码是:

<?php

$host="XXXXXXXXXXXXXX"; 
$username="aXXXXX62";
$password="XXXXXX";
$db_name="XXXXXn";
$tbl_name="mXXXXs";


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);


$count=mysql_num_rows($result);


if($count==1){
echo "User Found";
session_register("myusername");
session_register("mypassword"); 
}
else {
echo "Wrong Username or Password";
}
?>

这是我的java代码:

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    Button b,signup;
    EditText myusername,mypassword;
    TextView tv;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;
    ProgressDialog dialog = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        b = (Button)findViewById(R.id.login);
        signup = (Button) findViewById(R.id.signup);
        myusername = (EditText)findViewById(R.id.username);
        mypassword= (EditText)findViewById(R.id.password);
        tv = (TextView)findViewById(R.id.tv);

        signup.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent( getBaseContext(), signup.class));
            }
        });    





        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(MainActivity.this, "", 
                        "Validating user...", true);
                 new Thread(new Runnable() {
                        public void run() {
                            login();                          
                        }
                      }).start();               
            }
        });
    }

    void login(){
        try{            
            httpclient=new DefaultHttpClient();
            httppost= new HttpPost("http://getjobcompleted.info/checklogin.php"); 
            //add your data
            nameValuePairs = new ArrayList<NameValuePair>(2);
            // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
            nameValuePairs.add(new BasicNameValuePair("myusername",myusername.getText().toString().trim()));  ;
            nameValuePairs.add(new BasicNameValuePair("mypassword",mypassword.getText().toString().trim())); 
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //Execute HTTP Post Request
            response=httpclient.execute(httppost);
            // edited by James from coderzheaven.. from here....
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);
            System.out.println("Response : " + response); 
            runOnUiThread(new Runnable() {
                public void run() {
                    tv.setText("Response from PHP : " + response);
                    dialog.dismiss();
                }
            });

            if(response.equalsIgnoreCase("User Found")){
                 startActivity(new Intent(getBaseContext(), view_create_url.class));        
            }


        }catch(Exception e){
            dialog.dismiss();
            System.out.println("Exception : " + e.getMessage());
        }
    }
}

APP显示用户已通过身份验证但是何时重定向到下一页。 请告诉我需要改变什么?

1 个答案:

答案 0 :(得分:0)

您应该从响应中获取实体并从中创建输入流

HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();

然后从输入流中读取并检查所需的响应。