【问题标题】:Android inserting value in database in backgroundAndroid在后台在数据库中插入值
【发布时间】:2014-05-18 13:33:59
【问题描述】:

我正在使用 asyntask 在数据库中输入值。在做背景方法我正在调用这个方法。

           private void callLogin() {
             GetDataFromApi getDataFromApi = new GetDataFromApi(url);

             if (!isTableFalse) {
              Log.e("MAI A GAAYA SYNCRONISE DATA BASE MAI", "HA MAI AYA");
             String message = 
   getDataFromApi.postSignIn().toString().trim();
              syncroniseDatabase(message);
              populateChurchListOnValidating
                    .populateChurchList(parseJsonToArrayList());
            } else {
              try {
                 loginValue = 
              Integer.parseInt(getDataFromApi.postSignIn());
                     Log.e("Login VAlue", "" + loginValue);
               } catch (NumberFormatException nfe) {
                 nfe.printStackTrace();
             }
         }
         }

而我的 syncroniseDatabase(message) 是这样的

      private void syncroniseDatabase(String mesString) {

Log.e("同步中的 URL", ""+ url);

              try {
               InsertTable insertTable = new InsertTable();
               JSONObject jsonObject = new JSONObject(mesString);
      insertTable.addRowforMemberTable(jsonObject.getString(
      RegistrationAppConstant.MEMBER_ID),

        jsonObject.getString(RegistrationAppConstant.CLIENT_ID),

        jsonObject.getString(RegistrationAppConstant.FIRSTNAME),

        jsonObject.getString(RegistrationAppConstant.SURNAME),

        jsonObject.getString(RegistrationAppConstant.EMAIL_ID),

       jsonObject.getString(RegistrationAppConstant.PASSWORD));
               Log.e("Inserting Data DONE", "" + "Done");
           } catch (JSONException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }

而我的forInsertTable类是这样的

        public class InsertTable {

       public void addRowforMemberTable(String memberID, 
           String clientID, String 
               firstName,String surName, String emailIDString, String passWordString) 
            {
           Log.e("NAME", "" + memberID + "  " + clientID + " " + firstName + " "
                + surName + " " + emailIDString + " " + 
                  passWordString);

           ContentValues contentValue = new ContentValues();

         contentValue.put(AppConstant.MCM_MEMBER_MEMEBER_ID, memberID);
         contentValue.put(AppConstant.MCM_MEMBER_CLIENT_ID, clientID);
         contentValue.put(AppConstant.MCM_MEMBER_FIRST_NAME, firstName);
          contentValue.put(AppConstant.MCM_MEMBER_LAST_NAME, surName);
          contentValue.put(AppConstant.MCM_MEMBER_EMAIL_ID, emailIDString);
          contentValue.put(AppConstant.MCM_MEMBER_PASSWORD, passWordString);
         Log.e("Cotent value", "" + contentValue);

          try {

      SplashActivity.databaseHelper.insertInToTable(
      SplashActivity.databaseHelper.getWritableDatabase(),
                        AppConstant.MEMBER_TABLE_NAME,  
                            contentValue);
                    }

            catch (Exception e) {
        e.printStackTrace();
          }
       }
       }

这是我的 json 字符串

[{"MemberId":77,"ClientId":37,"FirstName":"John","SurName":"Banian","Address1":null,"Address2":null,"Street":null ,"城镇":null,"城市":null,"县":null,"州":null,"国家":null,"邮政编码":null,"手机":null,"家庭电话":null," WorkTelephone":null,"EmailId":"jbunian@yahoo.com","ConfirmEmailId":null,"Password":"123","Age":null,"Sex":null,"Profession":null," MartialStatus":null,"国籍":null,"Children":null,"ChurchMembershipId":null,"SecurityQuestion":null,"SecurityAnswer":null,"IsApproved":null,"IsLockedOut":null,"CreateDate" :null,"LastLoginDate":null,"LastPasswordChangedDate":null,"LastLogOutDate":null,"FailedPasswordAttemptDate":null,"FailedPasswordAttemptWindowStart":null,"FailedPasswordAnswerAttemptCount":null,"FailedPasswordAnswerAttemptWindowStart":null,"Comment":null "活动":null,"IsAuthToApproveNewMember":null,"Record_Created":null,"Record_Updated":null,"LastUpdated_LoginUserID":null,"LastUpdated_WindowsUser":null,"ClientAdminEmailId":null,"EmailListForApproval":null ,"AppRegistrationStatus":null,"MemberEmailMessage":null,"AdminEmailMessage":null,"PhysicalDeviceId":null,"DeviceOS":null,"DeviceIdFromGCMorAPNS":null,"DeviceType":null}]

但是我的代码在这一行之后不会执行,因此我无法插入值。我将日志加粗,之后它没有执行。请告诉它为什么没有执行?

【问题讨论】:

  • 你读过logcat吗?这里有例外吗?
  • logcat中也不例外。
  • 您看到这个 - “同步中的 URL”,但没有看到这个“插入数据完成”,这里也没有例外?那么这是一个奇迹,你必须向教皇报告。
  • 我发现它说 json 数组类型的值不能转换为 json 字符串。这是为什么呢?
  • 添加 Log.d("JSON 字符串", mesString);并添加有问题的 JSON 字符串。

标签: android


【解决方案1】:

问题是您试图从 JSONObject 获取值,但实际上它是 JSON 数组。 (json 字符串以 [.

开头

尝试做这样的事情:

      JSONArray jsonArray = new JSONArray(mesString);
      JSONObject jsonObject = jsonArray.get(0);
      insertTable.addRowforMemberTable(jsonObject.getString(RegistrationAppConstant.MEMBER_ID),
jsonObject.getString(RegistrationAppConstant.CLIENT_ID),
.....
.....

【讨论】:

    猜你喜欢
    • 2023-03-02
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2013-06-18
    • 2012-09-09
    • 2013-04-17
    相关资源
    最近更新 更多