2015-01-21 73 views
0

我想保存從服務器等等都使用的共享偏好,但我得到的JSON類型不匹配錯誤,並不能轉移到其他活動也響應。請幫忙!!!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":"[email protected]", 
     "password":"7a4fb4770d2c404b0c48abd49f4c5f6a", 
     "id":"118" 
     } 
} 
{ 
    "error":0, 
    "user":{ 
     "id":"118", 
     "password":"7a4fb4770d2c404b0c48abd49f4c5f6a", 
     "email":"[email protected]" 
     }, 
     "success":1, 
     "tag":"login" 
} 
+0

的JSONObject jsonObject1 = JSONObject的 .getJSONObject( 「用戶」);應該是 JSONObject jsonObject1 = jsonObject .getJSONObject(「email」); 沒有這樣的JSON標籤 – 2015-01-21 06:36:13

回答

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"); 
+0

我如何可以檢索用戶「身份證」,我有她的shraed偏好,而我loginactvity – 2015-01-21 06:43:37

+0

感謝上述溶液。 – 2015-01-21 06:45:46

+0

使用這個'String id = jsonObject1.getString(「id」);'。請正確檢查我的答案。實際上,您已經創建了額外的jsonobject實例,無需這樣做。 – Piyush 2015-01-21 06:46:24