我正在使用带有android的Volley库并从我的服务器获取一个String对象的响应,但无法正确比较它。我正在尝试以下方法:
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www...";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("from onResponse()", response);
if(response.equals("no_such_table")){
//do something
}else{
//do something else
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
onTaskFailed(error);
}
})
android监视器正在打印出预期的字符串(“no_such_table”),但是其他情况正在被触发。从具有齐射库的服务器返回的响应的编码是否存在问题?
我也试过
response.toString().equals("no_such_table")
String.valueOf(response).equals("no_such_table")
"no_such_table".equals(response)
"no_such_table".equals(response.toString())
但我无法按预期工作
响应由服务器上的php脚本提供
$val = mysql_query('select 1 from `" . $device_group_id . "` LIMIT 1');
if($val == FALSE){
//table does not exist so return message
echo "no_such_table";
}
答案 0 :(得分:0)
首先将您的回复评估为:
@Override
public void onResponse(String response) {
if(response != null && response.equals("expected_string")){
//print log of response when it isnt a null value
Log.d("from onResponse()", "datas = " + response);
//do something
}else{
Log.d("from onResponse()", "null response");
//do something else
}
}
答案 1 :(得分:0)
尝试
"expected_string".equals(response)
或
"expected_string".equals(response.toString())
答案 2 :(得分:0)
@Override
public void onResponse(String response) {
Log.d("from onResponse()", response);
if(("expected_string").equals(response.toString())){
//do something
}else{
//do something else
}
}
答案 3 :(得分:0)
我遇到了同样的问题,我设法解决了!
尝试使用.trim()
方法和字符串。
@Override
public void onResponse(String response) {
Log.d("from onResponse()", response);
response = response.trim();
if(response.equals("no_such_table")){
//do something
}else{
//do something else
}
}