适用于Android程序的SQLite,JSON错误(Error in SQLite, JSON, for Android Program)

编程入门 行业动态 更新时间:2024-10-25 18:24:00
适用于Android程序的SQLite,JSON错误(Error in SQLite, JSON, for Android Program)

我想要实现的是我正在处理的应用程序的注册脚本,一旦用户单击注册它使用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 dave

I 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

更多推荐

本文发布于:2023-07-15 21:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   错误   程序   Android   SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!