我有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的查询很好,但是&#34; if&#34;总是返回&#34; no&#34;,但如果我把变量&#34; u&#34;在祝酒词上,它返回&#34; 1&#34;当存在用户并传递时,&#34; 0&#34;什么时候不存在。
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?>
答案 0 :(得分:0)
尝试这样的事情
if(Integer.valueOf(u) == 1)
希望这会对你有所帮助。
答案 1 :(得分:0)
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();
}
}
上面的代码是你的代码,但是稍微进行了优化,并且更易于阅读。
显然,u
不等于"1"
。这可能是因为u
中您没有看到的字符,例如空格。
这意味着你的if条件完美无瑕。你应该关注u
是什么。您可以通过调试我放入代码中的语句来实现。
更新
12-12 11:38:52.858: I/System.out(4445): length 5
12-12 11:38:52.858: I/System.out(4445): '1 '
12-12 11:38:52.862: I/System.out(4445): current = 49
12-12 11:38:52.862: I/System.out(4445): current = 9
12-12 11:38:52.862: I/System.out(4445): current = 9
12-12 11:38:52.862: I/System.out(4445): current = 9
12-12 11:38:52.862: I/System.out(4445): current = 9
and the toast shows "no" –
查看日志语句,会在响应中添加一些水平制表符。
由于第一个条目是49
,您可以使用
return u.length() > 0 && u.charAt(0) == '1';
这应该可以解决您的问题,但您仍然需要找出发生这种情况的原因
修改
您可以尝试将EntityUtils.toString
与字符集EntityUtils.toString(entity, "UTF-8)"
答案 2 :(得分:0)
响应中也必须出现空格或某个不可见的字符。
试试这个:
if(Pattern.compile("1").matcher(u).find())