2016-04-02 37 views
-1

我正在制作android应用程序与gcm集成聊天和消息广播。但是,当我执行它,应用程序显示错误:如何调试“json解析错误:错误值为true的java.lang.Boolean错误值不能转换为JSONObject”

json parsing error: Value true at error of type java.lang.Boolean cannot be converted to JSONObject

LoginActivity.java

public class LoginActivity extends AppCompatActivity { 

    private String TAG = LoginActivity.class.getSimpleName(); 
    private EditText inputName, inputEmail; 
    private TextInputLayout inputLayoutName, inputLayoutEmail; 
    private Button btnEnter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     /** 
     * Check for login session. It user is already logged in 
     * redirect him to main activity 
     * */ 
     if (MyApplication.getInstance().getPrefManager().getUser() != null) { 
      startActivity(new Intent(this, MainActivity.class)); 
      finish(); 
     } 

     setContentView(R.layout.activity_login); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     inputLayoutName = (TextInputLayout) findViewById(R.id.input_layout_name); 
     inputLayoutEmail = (TextInputLayout) findViewById(R.id.input_layout_email); 
     inputName = (EditText) findViewById(R.id.input_name); 
     inputEmail = (EditText) findViewById(R.id.input_email); 
     btnEnter = (Button) findViewById(R.id.btn_enter); 

     inputName.addTextChangedListener(new MyTextWatcher(inputName)); 
     inputEmail.addTextChangedListener(new MyTextWatcher(inputEmail)); 

     btnEnter.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       login(); 
      } 
     }); 
    } 

    /** 
    * logging in user. Will make http post request with name, email 
    * as parameters 
    */ 
    private void login() { 
     if (!validateName()) { 
      return; 
     } 

     if (!validateEmail()) { 
      return; 
     } 

     final String name = inputName.getText().toString(); 
     final String email = inputEmail.getText().toString(); 

     StringRequest strReq = new StringRequest(Request.Method.POST, 
       EndPoints.LOGIN, new Response.Listener<String>() { 

      @Override 
      public void onResponse(String response) { 
       Log.e(TAG, "response: " + response); 

       try { 



        JSONObject obj = new JSONObject(response); 

        // check for error flag 
        if (obj.getBoolean("error") == false) { 
         // user successfully logged in 

         JSONObject userObj = obj.getJSONObject("user"); 
         User user = new User(userObj.getString("user_id"), 
           userObj.getString("name"), 
           userObj.getString("email")); 

         // storing user in shared preferences 
         MyApplication.getInstance().getPrefManager().storeUser(user); 

         // start main activity 
         startActivity(new Intent(getApplicationContext(), MainActivity.class)); 
         finish(); 

        } else { 
         // login error - simply toast the message 
         Toast.makeText(getApplicationContext(), "" + obj.getJSONObject("error").getString("message"), Toast.LENGTH_LONG).show(); 
        } 

       } catch (JSONException e) { 
        Log.e(TAG, "json parsing error: " + e.getMessage()); 
        Toast.makeText(getApplicationContext(), "Json parse error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       NetworkResponse networkResponse = error.networkResponse; 
       Log.e(TAG, "Volley error: " + error.getMessage() + ", code: " + networkResponse); 
       Toast.makeText(getApplicationContext(), "Volley error: " + error.getMessage(), Toast.LENGTH_SHORT).show(); 
      } 
     }) { 

      @Override 
      protected Map<String, String> getParams() { 
       Map<String, String> params = new HashMap<>(); 
       params.put("name", name); 
       params.put("email", email); 

       Log.e(TAG, "params: " + params.toString()); 
       return params; 
      } 
     }; 

     //Adding request to request queue 
     MyApplication.getInstance().addToRequestQueue(strReq); 
    } 

    private void requestFocus(View view) { 
     if (view.requestFocus()) { 
      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
     } 
    } 

    // Validating name 
    private boolean validateName() { 
     if (inputName.getText().toString().trim().isEmpty()) { 
      inputLayoutName.setError(getString(R.string.err_msg_name)); 
      requestFocus(inputName); 
      return false; 
     } else { 
      inputLayoutName.setErrorEnabled(false); 
     } 

     return true; 
    } 

    // Validating email 
    private boolean validateEmail() { 
     String email = inputEmail.getText().toString().trim(); 

     if (email.isEmpty() || !isValidEmail(email)) { 
      inputLayoutEmail.setError(getString(R.string.err_msg_email)); 
      requestFocus(inputEmail); 
      return false; 
     } else { 
      inputLayoutEmail.setErrorEnabled(false); 
     } 

     return true; 
    } 

    private static boolean isValidEmail(String email) { 
     return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches(); 
    } 

    private class MyTextWatcher implements TextWatcher { 

     private View view; 
     private MyTextWatcher(View view) { 
      this.view = view; 
     } 

     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
     } 

     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
     } 

     public void afterTextChanged(Editable editable) { 
      switch (view.getId()) { 
       case R.id.input_name: 
        validateName(); 
        break; 
       case R.id.input_email: 
        validateEmail(); 
        break; 
      } 
     } 
    } 

} 
+3

发布response'的'值。 –

+0

请阅读http://stackoverflow.com/questions/32261410/receiving-boolean-variable-with-json-request-in-volley/32261896#32261896 – BNK

回答

0

因为response你得到它的格式不正确的JSON的转换。

JSONObject obj = new JSONObject(response); 

其返回boolean您正试图转换成JSONObject

编辑1:它也可能发生在取回boolean时出现问题。

尝试像这样取,

jsonObject.optBoolean("error"); 
+0

所以,我添加了什么,以便它可以转换成JSONObject? –

+0

你正在做正确的事情,但凌乱是返回布尔值不是JSON值。打印回复,看看这是你的期望? –

+0

这将是更有帮助的兄弟,如果你可以帮我一个代码镜头, –

相关问题