2013-04-15 60 views
-1

我在我的應用程序中創建了登錄頁面,但當用戶輸入正確的用戶並將其顯示在屏幕上顯示用戶已找到但應用程序未重定向到下一個佈局時,我遇到了問題。登錄後應用程序不會重定向到下一頁

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表明用戶進行身份驗證,但當但它重定向到下一個頁面。 請讓我知道需要改變什麼?

回答

0

你應該得到的響應實體,並創建一個輸入流,從它

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

然後從輸入流中讀取並檢查所需要的響應。

相關問題