dougaoshang0237 2014-03-30 04:06
浏览 33
已采纳

将php脚本(执行查询以显示消息时)的返回结果与我的字符串对象进行比较时出错

I have a mainactivity which is inserting data into MySQL db using PHP. The insertion process is working fine. I have kept yes for success and no for fail in PHP script. And I'm getting the correct result from PHP. but when I compare like if(result=="yes")//do else//do another.

its not working only else part is working eventhough result from PHP is yes.

here is my mainactivity.java

 package com.example.demo;

    import java.io.BufferedReader;

    import java.io.InputStream;
    import java.io.InputStreamReader;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;

    import org.apache.http.client.HttpClient;

    import org.apache.http.client.methods.HttpGet;

    import org.apache.http.impl.client.DefaultHttpClient;

    import android.support.v7.app.ActionBarActivity;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;

    import android.view.View;
    import android.view.View.OnClickListener;

    import android.widget.Button;
    import android.widget.EditText;

    import android.widget.Toast;

    public class MainActivity extends ActionBarActivity {
        String em, pw, un, mb, msg = null;
        EditText t1, t2, t3, t4;
        Button b;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            t1 = (EditText) findViewById(R.id.editText1);
            t2 = (EditText) findViewById(R.id.editText2);
            t3 = (EditText) findViewById(R.id.editText3);
            t4 = (EditText) findViewById(R.id.editText4);
            b = (Button) findViewById(R.id.button1);

            b.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    un = t1.getText().toString();
                    pw = t2.getText().toString();
                    em = t3.getText().toString();
                    mb = t4.getText().toString();
                    new LoginAsync()
                            .execute("http://androide.netne.net/register.php?un="
                                    + un + "&em=" + em + "&pw=" + pw + "&mb=" + mb
                                    + ")");

                }
            }

            );
        }

        private class LoginAsync extends AsyncTask<String, Void, String> {

            String line, result;

            InputStream is = null;

            @Override
            protected String doInBackground(String... params) {
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpGet httpget = new HttpGet(params[0]);
                    HttpResponse response = httpclient.execute(httpget);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                    Log.e("pass 1", "connection success ");
                } catch (Exception e) {
                    Log.e("Fail 1", e.toString());
                }
               try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "
");
                    }
                    is.close();
                    result = sb.toString();

                    if (result.equals("no")) {
                        msg = "email id already taken";
                    } else {
                        msg = "Registered successfully";
                    }
                    Log.e("pass 2", "connection success " + result);
                } catch (Exception e) {
                    Log.e("Fail 2", e.toString());
                }
                return msg;

            }

            @Override
            protected void onPostExecute(String result) {
                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT)
                        .show();
            }

        }

    }

here is my php file

<?php
$name = $_GET['un'];
$email = $_GET['em'];
$password = $_GET['pw'];
$mobile = $_GET['mb'];
$con=mysqli_connect("DBHOST","DBUSER","DBPASSWORD","DBNAME");
if (mysqli_connect_errno($con))
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO users(name,email,password,mobile) VALUES ('$name','$email','$password','$mobile')";
if (mysqli_query($con,$sql))
{
   echo "yes";
}
else{
 echo "no";
}
?>
  • 写回答

1条回答 默认 最新

  • drju37335 2014-03-30 04:13
    关注

    To see if two strings are equal use firstString.compareTo(anotherString) == 0. Using == or .equals() tests if the objects are the same, not the contained string.

    For your case you would do if (result.compareTo("no") == 0).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题