JSON类型不匹配

时间:2015-01-21 06:24:12

标签: android json sharedpreferences

我想保存来自服务器的响应,因此使用了共享首选项,但我收到了JSON类型不匹配错误,也无法转移到其他活动。请帮忙!!!

Button login;
Button register;
EditText pass_word;
EditText mail;
TextView loginErrormsg;

private static final String TAG = null;

private static String KEY_SUCCESS = "success";
private static String KEY_UID = "uid";
private static String KEY_EMAIL = "email";
private static String KEY_PASSWORD = "password";

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

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    setContentView(R.layout.activity_main);

    mail = (EditText) findViewById(R.id.email);
    pass_word = (EditText) findViewById(R.id.password);
    loginErrormsg = (TextView) findViewById(R.id.login_error);
    login = (Button) findViewById(R.id.btnlogin);
    register = (Button) findViewById(R.id.btnregister);

    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String email = mail.getText().toString();
            String password = pass_word.getText().toString();
            UserFunctions userFunctions = new UserFunctions();
            JSONObject json = userFunctions.loginUser(email, password);
            // JSONObject userinfo = json.getJSONObject("user");

            // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    loginErrormsg.setText("");
                    String res = json.getString(KEY_SUCCESS);

                    if (Integer.parseInt(res) == 1) {
                        Log.d("Login Successful!", json.toString());

                        JSONObject jsonObject = new JSONObject("user");
                        JSONObject jsonObject1 = jsonObject
                                .getJSONObject("user");
                        String id = jsonObject1.getString("id");

                        SharedPreferences sp = getApplicationContext()
                                .getSharedPreferences("sharedPrefName",
                                        Context.MODE_PRIVATE); 

                        Editor editor = sp.edit();

                        // key_name(userid) is the name through which you can retrieve it later.
                        editor.putString("userid", id); 
                        editor.commit();

                        // user successfully logged in
                        // Store user details in SQLite Database
                        DatabaseHandler db = new DatabaseHandler(
                                getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        // Clear all previous data in database
                        userFunctions.logoutUser(getApplicationContext());
                        db.addUser(json_user.getString(KEY_EMAIL),
                                json_user.getString(KEY_PASSWORD));

                        Intent dashboard = new Intent(
                                getApplicationContext(),
                                LoginActivity.class);
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(dashboard);

                        // Close Login Screen
                        finish();
                    } else {
                        // Error in login
                        loginErrormsg
                                .setText("Incorrect username/password");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

    register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(getApplicationContext(),
                    RegisterActivity.class);
            startActivity(i);

        }
    });

}

public static void setUserObject(Context c, String userObject, String key) {
    SharedPreferences pref = PreferenceManager
            .getDefaultSharedPreferences(c);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString(key, userObject);
    editor.commit();
}

public static String getUserObject(Context ctx, String key) {
    SharedPreferences pref = PreferenceManager
            .getDefaultSharedPreferences(ctx);
    String userObject = pref.getString(key, null);
    return userObject;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

    }

logcat显示在以下响应中:

{
    "tag":"login",
    "success":1,
    "error":0,
    "user":{
        "email":"sridhar@gmail.com",
        "password":"7a4fb4770d2c404b0c48abd49f4c5f6a",
        "id":"118"
        }
}
 {
    "error":0,
    "user":{
        "id":"118",
        "password":"7a4fb4770d2c404b0c48abd49f4c5f6a",
        "email":"sridhar@gmail.com"
        },
        "success":1,
        "tag":"login"
}

1 个答案:

答案 0 :(得分:0)

你这里做错了

 JSONObject jsonObject = new JSONObject("user");
 JSONObject jsonObject1 = jsonObject
                            .getJSONObject("user");

您的第一个标记是JSONObject此处

 JSONObject json = userFunctions.loginUser(email, password);

无需在此处创建JSONObject的实例

 JSONObject jsonObject = new JSONObject("user");

所以解决方案将是

JSONObject jsonObject1 = json.getJSONObject("user");