如何使用json android从mysql数据库到textview获取特定值

时间:2019-04-20 11:53:47

标签: php android mysql json

它总是显示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"}]}

1 个答案:

答案 0 :(得分:0)

 String s = ob.getString("username").trim();
 if(s.equals("fasil")) {
    txt.setText(s);
 } else {
    Toast.makeText(profile.this, "not available", Toast.LENGTH_LONG).show();
 }