我想要实现的是我正在处理的应用程序的注册脚本,一旦用户单击注册它使用php更新外部mysql数据库(工作正常),然后还存储到设备上的本地sqllite数据库。 我得到的错误是这样的:
04-01 11:09:30.553 8592-8667/com.zelphe.zelpheapp E/JSON﹕ {"tag":"register","success":1,"error":0,"user":{"fname":"dave","lname":"test","email":"test@gmail.com"}} 04-01 11:09:30.613 8592-8592/com.zelphe.zelpheapp W/System.err﹕ org.json.JSONException: No value for dave我有点失去这实际意味着/我必须解决的问题?
dave应该没有值,因为dave是值? 据我所知,注册已经很好了?
编辑 - 已添加代码
好的,这是卡住时正在运行的活动
if (Integer.parseInt(res) == 1) { pDialog.setMessage("Loading User Space"); pDialog.setTitle("Getting Data"); DatabaseHandler db = new DatabaseHandler(getApplicationContext()); JSONObject json_user = json.getJSONObject("user"); /** * Clear all previous data in SQlite database. **/ UserFunctions logout = new UserFunctions(); logout.logoutUser(getApplicationContext()); db.addUser(json_user.getString(userFirstName), json_user.getString(userLastName), json_user.getString(userEmail)); /** *If JSON array details are stored in SQlite it launches the User Panel. **/ Intent upanel = new Intent(getApplicationContext(), MainActivity.class); upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); pDialog.dismiss(); startActivity(upanel); /** * Close Login Screen **/ finish();注销用户:
public boolean logoutUser(Context context){ DatabaseHandler db = new DatabaseHandler(context); db.resetTables(); return true; }添加用户:
public void addUser(String userFirstName, String userLastName, String userEmail) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_FIRSTNAME, userFirstName); // FirstName values.put(KEY_LASTNAME, userLastName); // LastName values.put(KEY_EMAIL, userEmail); // Email // Inserting Row db.insert(TABLE_LOGIN, null, values); db.close(); // Closing database connection }What I am trying to achieve is a registration script for an app I am working on, once the user clicks register it updates an external mysql database using php (works fine) and then also stores into a local sqllite database on the device. The error I am getting is this:
04-01 11:09:30.553 8592-8667/com.zelphe.zelpheapp E/JSON﹕ {"tag":"register","success":1,"error":0,"user":{"fname":"dave","lname":"test","email":"test@gmail.com"}} 04-01 11:09:30.613 8592-8592/com.zelphe.zelpheapp W/System.err﹕ org.json.JSONException: No value for daveI am at a bit of a loss of what this actually means / I have to fix?
there shouldn't be a value for dave as dave is the value? As far as I can see the registration has gone fine?
Edit - Code Added
Ok, this is the activity that is running when it gets stuck
if (Integer.parseInt(res) == 1) { pDialog.setMessage("Loading User Space"); pDialog.setTitle("Getting Data"); DatabaseHandler db = new DatabaseHandler(getApplicationContext()); JSONObject json_user = json.getJSONObject("user"); /** * Clear all previous data in SQlite database. **/ UserFunctions logout = new UserFunctions(); logout.logoutUser(getApplicationContext()); db.addUser(json_user.getString(userFirstName), json_user.getString(userLastName), json_user.getString(userEmail)); /** *If JSON array details are stored in SQlite it launches the User Panel. **/ Intent upanel = new Intent(getApplicationContext(), MainActivity.class); upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); pDialog.dismiss(); startActivity(upanel); /** * Close Login Screen **/ finish();logout user:
public boolean logoutUser(Context context){ DatabaseHandler db = new DatabaseHandler(context); db.resetTables(); return true; }adduser:
public void addUser(String userFirstName, String userLastName, String userEmail) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_FIRSTNAME, userFirstName); // FirstName values.put(KEY_LASTNAME, userLastName); // LastName values.put(KEY_EMAIL, userEmail); // Email // Inserting Row db.insert(TABLE_LOGIN, null, values); db.close(); // Closing database connection }最满意答案
尝试这个..
{ //Main JSONObject "tag": "register", "success": 1, "error": 0, "user": { //User JSONObject "fname": "dave", "lname": "test", "email": "test@gmail.com" } }码:
JSONObject json = new JSONObject(response); Log.v("tag",""+json.getString("tag")); Log.v("success",""+json.getString("success")); Log.v("error",""+json.getString("error")); JSONObject JObj = json.getJSONObject("user"); Log.v("fname",""+JObj.getString("fname")); Log.v("lname",""+JObj.getString("lname")); Log.v("email",""+JObj.getString("email"));Well it appears that it was actually a lot simplier than I was thinking:
db.addUser(json_user.getString(userFirstName), json_user.getString(userLastName), json_user.getString(userEmail));this is asking for the keys, where I was sending the values!
should be
db.addUser(json_user.getString(KEY_UID),json_user.getString(KEY_FIRSTNAME), json_user.getString(KEY_LASTNAME), json_user.getString(KEY_EMAIL));I have also added a uid key here now as well which was not in the original
更多推荐
发布评论