【发布时间】:2018-03-27 08:17:51
【问题描述】:
所以,我正在尝试为我的 Android 应用实现一个简单的登录。
我有一个 php 文件,它可以进行 SQL 查询并像这样处理结果:
if($count == 1)
{
echo "success";
}else{
echo "denied";
}
在我的 android 应用上,带有 http 请求的(相关)代码以及响应:
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 语句没有返回 true。
另一方面,与
System.out.println("Response : " + response);
如果用户确实存在于数据库中,则响应被写为“成功”。
可能我没有以最好的方式访问响应内容,所以我正在就如何正确执行它并使其通过条件请求建议。
事实:
我可以成功地与数据库通信。 如果用户存在,则响应为“成功”。 我在显示“成功”的文本视图中编写响应。 Log.w,就在 if 语句之后没有运行。
非常感谢!
【问题讨论】:
-
调试并检查response的值
-
System.out.println("Response : " + response);他做到了 -
确保响应中没有空格字符或类似字符。也许尝试将您的逻辑更改为
if(response.contains("success"))进行测试 -
嘿,刚刚用
if(response.contains("success"))尝试过,它成功了。谢谢!
标签: java android string compare httprequest