在的Java / Android的HTTP响应比较字符串 [英] Comparing string from http response in Java/Android

查看:161
本文介绍了在的Java / Android的HTTP响应比较字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想实现我的Andr​​oid应用一个简单的登录。

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

我有一个PHP文件,使SQL查询和处理这样的结果:

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

if($count == 1)
{

    echo "success";

}else{

    echo "denied";
}

在我的Andr​​oid应用程序,在(有关)code与HTTP请求,从而响应:

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);
        } 

的问题是,在if语句,用于比较与成功的响应没有返回正确的。

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

在另一方面,以

 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.

事实:

我能成功地与数据库进行通讯。
如果用户存在的响应是成功。
我正在写在显示成功一个TextView的响应。
该Log.w,右后if语句没有运行。

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.

提前感谢!

推荐答案

对于那些谁碰到过类似的问题,@Ken狼的回答没有的伎俩。

For those who come across a similar problem, @Ken Wolf 's answer did the trick.

if(response.contains("success")) 

这篇关于在的Java / Android的HTTP响应比较字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆