Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果(条件)在异步任务中不起作用_Android_Json_Android Asynctask - Fatal编程技术网

Android 如果(条件)在异步任务中不起作用

Android 如果(条件)在异步任务中不起作用,android,json,android-asynctask,Android,Json,Android Asynctask,我用asynctask和json编写了以下代码: class loginn extends AsyncTask<Void, Void, Void>{ String u; String usuario = user.getText().toString(); String contraseña = pass.getText().toString(); @Override protected Void doInBackgrou

我用asynctask和json编写了以下代码:

class loginn extends AsyncTask<Void, Void, Void>{
     String u;
     String usuario = user.getText().toString();
     String contraseña = pass.getText().toString();
        @Override
        protected Void doInBackground(Void... params) {


                HttpClient httpclient2 = new DefaultHttpClient();
                HttpPost httppost2 = new HttpPost("****");

                     try
                     {
                     nameValuePairs = new ArrayList<NameValuePair>();
                     nameValuePairs.add(new BasicNameValuePair("user", usuario));
                     nameValuePairs.add(new BasicNameValuePair("pass", contraseña));                     
                     httppost2.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     HttpResponse response2 = httpclient2.execute(httppost2);
                     HttpEntity entity = response2.getEntity();
                     u = EntityUtils.toString(entity);
                     if(u.equals("1")){

                     runOnUiThread(new Runnable() {
                        public void run() {
                            Toast toast = Toast.makeText(login.this, "yes", Toast.LENGTH_SHORT);
                             toast.show();
                        }
                        });
                             }else
                             {
                                 runOnUiThread(new Runnable() {
                                    public void run() {
                                        Toast toast = Toast.makeText(login.this, "no", Toast.LENGTH_SHORT);
                                         toast.show();
                                    }
                                    });
                             }
                 }
                 catch(Exception e)
                 {

                 }
                    return null;


        }

        protected void onPostExecute(Void result) {

            }
php的查询很好,但是if总是返回no,但是如果我将变量u放在toast上,当用户和pass存在时,它会返回1,当不存在时,它会返回0

php是:

<?php  

$con = mysql_connect('***', '****', '*****');  
mysql_query("SET CHARACTER SET utf8");  
mysql_query("SET NAMES utf8");  
$user = $_POST['user'];
$pass = $_POST['pass'];
if( $con )  
{  
mysql_select_db('*****');  
$query = "select * from usuarios where username='$user' and passw='$pass'";    
$res = mysql_query($query);
$count = 0;

    while($row = mysql_fetch_object($res))
      {
         $count++;
      }
if($count==1)
{
   echo json_encode(1);
}else
  {
    echo json_encode(0);
  }
}
php?>

试试这样的

if(Integer.valueOf(u) == 1)
希望这对你有帮助

class loginn extends AsyncTask<Void, Void, Boolean>{
  String usuario = user.getText().toString();
  String contraseña = pass.getText().toString();

  @Override
  protected Boolean doInBackground(Void... params) {
    HttpClient httpclient2 = new DefaultHttpClient();
    HttpPost httppost2 = new HttpPost("****");
    try {
      nameValuePairs = new ArrayList<NameValuePair>();
      nameValuePairs.add(new BasicNameValuePair("user", usuario));
      nameValuePairs.add(new BasicNameValuePair("pass", contraseña));                     
      httppost2.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response2 = httpclient2.execute(httppost2);
      HttpEntity entity = response2.getEntity();
      String u = EntityUtils.toString(entity);
      System.out.println("length " + u.length()); //should be 1
      System.out.println("'" + u + "'"); //should be '1'
      for (char c : u.toCharArray())
        System.out.println("current = " + ((int)c)); //should be 49, if more than one => look up in ASCII table
      return u.equals("1");
    }
    catch(Exception e) { 
      return false;
    }
  }

  protected void onPostExecute(Boolean result) {
    Toast.makeText(login.this, result ? "yes" : "no", Toast.LENGTH_SHORT).show();
  }
}
看到日志语句后,一些水平选项卡会添加到响应中

因为第一个条目是49,所以可以使用

return u.length() > 0 && u.charAt(0) == '1';
这应该可以解决你的问题,但你仍然需要找出为什么会发生这种情况

编辑


您可以尝试将EntityUtils.toString与字符集一起使用:EntityUtils.toString、UTF-8

响应中还必须存在空格或某些不可见字符

试试这个:

if(Pattern.compile("1").matcher(u).find())

尝试equalsIgnoreCase并检查其是否工作。尝试Integer.parseIntu==1或EqualIgnoreCase记录响应时会发生什么:主题的|,但不要在AsyncTask内启动新的Runnable。在onPostExecute中进行UI更改。它仍然返回否,我使用新的loginn.execute在onclick方法中调用asyncktask;12-12 11:38:52.858:I/System.out4445:length 512-12 11:38:52.858:I/System.out4445:'1'12-12 11:38:52.862:I/System.out4445:current=49 12-12 11:38:52.862:I/System.out4445:current=9 12-12 11:38:52.862:I/System.out4445:current=9 12-12-12 11:38:52.862:I/System祝酒词没有显示出什么意思
if(Pattern.compile("1").matcher(u).find())