【发布时间】:2019-04-20 11:53:47
【问题描述】:
它总是显示 else 语句
StringRequest stringRequest=new StringRequest(Request.Method.GET, HI, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
String f="fasil";
TextView txt=(TextView)findViewById(R.id.nam);
JSONObject jsonObject=new JSONObject(response);
JSONArray array=jsonObject.getJSONArray("data");
for (int i=0;i<array.length();i++) {
JSONObject ob = array.getJSONObject(i);
String s = ob.getString("username").trim();
if(s==f) {
String text = "" + s;
txt.setText(text);
}
else
Toast.makeText(profile.this,"not available",Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(profile.this,"error",Toast.LENGTH_LONG).show();
}
});
php结果
{"data":[{"username":"fasil","password":"fasilt"},{"username":"ubhh","password":"g g gvhv"},{"username":"ashique","password":"ashiqueash"},{"username":"zabir","password":"zabir1999"}]}
【问题讨论】:
-
不是 100% 关于 java,但
s==f和s.contentEquals(f)一样吗? -
这样试试 ->
if(s.equals(f)) -
调试时s和f变量的de值是多少?