简体   繁体   中英

Comparing string from http response in Java/Android

So, I'm trying to implement a simple login for my android app.

I have a php file that makes the SQL query and handles the results like this:

if($count == 1)
{

    echo "success";

}else{

    echo "denied";
}

On my android app, the (relevant) code with the http request and thus the response :

new Thread(new Runnable() {
 public void run() {
 login();                          
 }
}).start();   

void login(){
    try{            

        httpclient=new DefaultHttpClient();
        httppost= new HttpPost("http://myurl.com/login.php"); // make sure the url is correct.
        //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("mail",usermail.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
        nameValuePairs.add(new       BasicNameValuePair("password",userpass.getText().toString().trim())); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Execute HTTP Post Request
        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);
                dialog.dismiss();
            }
        });


        if(response.equals("success")){
            Log.w("IF STATEMENT", "VALID");
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(Login.this,"Login Success", Toast.LENGTH_SHORT).show();
                }
            }); 

            Intent userPage = new Intent("android.intent.action.UserPage");
            startActivity(userPage);
        } 

The problem is that the if statement that compares the response with "success" isn't returning true.

On the other hand, with the

 System.out.println("Response : " + response); 

the response, if indeed the user exists in the database, is being written as "success".

Probably I'm not accessing the response content in the best way so I'm asking advice on how to do it correctly and make it pass the conditional.

FACTS:

I can successfully communicate with the DB. If the user exists the response is "success". I'm writing the response in a textview that displays "success". The Log.w, right after the if statement isn't running.

Many thanks in advance!

对于遇到类似问题的人,@ Ken Wolf的答案可以解决问题。

if(response.contains("success")) 

我迟到5年了。

    if (2 == response.getStatusLine().getStatusCode() / 100)

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