Java 使用json处理和快速Android网络发布

Java 使用json处理和快速Android网络发布,java,android,json,Java,Android,Json,我正在发送一些服务器需要检查的post数据。 它返回一个JSON对象,如下所示: {"login":"true","error":"false"} 我想检查“login”的值。 这是我关于此任务的代码: btnLogin.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String email = inputEmail.getText().t

我正在发送一些服务器需要检查的post数据。 它返回一个JSON对象,如下所示:

{"login":"true","error":"false"}
我想检查
“login”
的值。 这是我关于此任务的代码:

btnLogin.setOnClickListener(new View.OnClickListener() {



        public void onClick(View v) {
            String email = inputEmail.getText().toString();
            String passw = inputPassword.getText().toString();
            pDialog.setMessage("Caricamento ...");
            showDialog();

                AndroidNetworking.post(AppConfig.URL_LOGIN)
                        .addHeaders("Content-Type", "application/json")
                        .addHeaders("Authorization", "Token token=cazzodigomma")
                        .addHeaders("Accept", "application/json")
                        .addBodyParameter("email", email)
                        .addBodyParameter("password", passw)
                        .setPriority(Priority.MEDIUM)
                        .build()
                        .getAsJSONObject(new JSONObjectRequestListener() {

                                             @Override
                                             public void onResponse(JSONObject response) {
                                                 hideDialog();
                                                 //Toast.makeText(getApplicationContext(),
                                                 //"Va tutto bene :D", Toast.LENGTH_SHORT).show();
                                                 Log.d(TAG, "JSON: " + response);
                                                 JSONObject jsobj = new JSONObject();
                                                 try {
                                                     String login_json = jsobj.getString("login");

                                                 } catch (JSONException e) {
                                                     e.printStackTrace();
                                                 }
                                                 if ("true".equals(login_json)) {
                                                     Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                                     startActivity(intent);
                                                     finish();
                                                 }


                                             }

                                             @Override
                                             public void onError(ANError error) {
                                                 // handle error
                                                 hideDialog();
                                                 Toast.makeText(getApplicationContext(),
                                                         "ERROR", Toast.LENGTH_SHORT).show();
                                             }
                                         }
                        );
        }
我想问题在于:

try {
     String login_json = jsobj.getString("login");

   } catch (JSONException e) {
     e.printStackTrace();
                             }
关于问题,我的意思是:

  • 如果
    login\u json
    中,请尝试{}
    我无法在if中使用它

  • 如果
    login\u json
    try{}
    Android Studio之外,则会出现一个未处理的异常错误


  • 您需要从Json对象中获取字符串作为响应,因此

    String login_json = jsobj.getString("login");
    


    这很简单。我所要做的就是搬家:

    if ("true".equals(login_json)) {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
    
    中尝试{}

    @Override
                                                 public void onResponse(JSONObject response) {
                                                     hideDialog();
                                                     //Toast.makeText(getApplicationContext(),
                                                     //"Va tutto bene :D", Toast.LENGTH_SHORT).show();
                                                     Log.d(TAG, "JSON: " + response);
                                                     JSONObject jsobj = new JSONObject();
    
                                                     try {
                                                         String login_json = response.getString("login");
                                                         if ("true".equals(login_json)) {
                                                             Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                                             startActivity(intent);
                                                             finish();
                                                         }
    
    
                                                     } catch (JSONException e) {
                                                         e.printStackTrace();
                                                     }
    
    
    
                                                 }
    

    不过,感谢你的回答和评论

    如果你告诉我们问题出在哪里,那会有帮助的。看起来您在与
    onResponse
    不同的范围内声明
    String login\u json
    ,因此无法在
    try catch
    之外访问它。要修复此问题,您可以在try-catch外部声明
    login\u json
    ,并在try-catch内部设置它的值。正确缩进代码如果我将login\u json放在try-catch外部,Android Studio会给我未处理的异常错误,这就是我将其放在原处的原因@nbokmansWell表示还有其他问题。
    login\u json
    的范围是
    try-catch
    块。您不能从
    onResponse
    块的作用域访问该变量-内部作用域可以访问其外部的变量,但不能通过其他方式访问有关作用域的更多信息这不是解决方案。解决方案就是@Deepesh Choudhary发布的内容。
    @Override
                                                 public void onResponse(JSONObject response) {
                                                     hideDialog();
                                                     //Toast.makeText(getApplicationContext(),
                                                     //"Va tutto bene :D", Toast.LENGTH_SHORT).show();
                                                     Log.d(TAG, "JSON: " + response);
                                                     JSONObject jsobj = new JSONObject();
    
                                                     try {
                                                         String login_json = response.getString("login");
                                                         if ("true".equals(login_json)) {
                                                             Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                                             startActivity(intent);
                                                             finish();
                                                         }
    
    
                                                     } catch (JSONException e) {
                                                         e.printStackTrace();
                                                     }
    
    
    
                                                 }