有没有一种方法可以使用String格式捕获JSON中的字段值?

时间:2020-04-28 15:58:28

标签: android mysql json android-volley

在代码中发送一个简单登录请求。 mysql正确地以保存在响应字符串中的JSON形式返回查询结果。现在,我需要获取响应的“角色”字段。有没有办法从字符串中提取它,还是必须使用JSONObetct?

   private void validarUsuario_servicio(String URL) {
    StringRequest stringRequest= new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            if(!response.isEmpty()){
                //obtenerRol_serivicio(response); HELP!
                guardarPreferencias();
                Intent i = new Intent(getApplicationContext(),menuActivity.class);
                startActivity(i);
                finish();
            }else{
                Toast.makeText(MainActivity.this, "Usuario o contraseña incorrectos", Toast.LENGTH_SHORT).show();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this,error.getMessage().toString(), Toast.LENGTH_SHORT).show(); //
        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> parametros = new HashMap<String,String>();
            parametros.put("usuario",usuario);
            parametros.put("password",password);
            return parametros;
        }
    };
    rq =Volley.newRequestQueue(this);

    try{
        rq.add(stringRequest);
    }catch(Exception e){
        Toast.makeText(this,e.getMessage().toString(),Toast.LENGTH_SHORT).show();
    }
}

0 个答案:

没有答案