我无法将JSON数据传输到数据库

时间:2016-04-23 07:12:40

标签: android json android-sqlite

我编写了一个例程,用于将数据从JSON传输到Android中的SQLite数据库,但出于某种原因,它没有被转移,我错过了什么? 这是我的OncreateInstance

 try
    {
        JSONObject jsonObject=new JSONObject(ParsingDta);
        JSONArray jsonArray= jsonObject.getJSONArray(ParsingDta);
        for(int i=0;i<jsonArray.length();i++)
        {
            JSONObject jsonObject1=jsonArray.getJSONObject(i);
            String rtrname=jsonObject1.getString("rtrname").toString();
            String ctgname=jsonObject1.getString("ctgname").toString();
            String rtrphoneno=jsonObject1.getString("rtrphoneno").toString();

            str+="\n Retailers"+i+ "\n rtrname:"+rtrname+"\n ctgname:"+ctgname+"\n rtrphoneno:" +rtrphoneno+"\n";
            boolean isInserted = database.insertData(rtrname,ctgname,rtrphoneno);
            if (isInserted == true)
                Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
            //textView1.setText(str);
        }
    }
    catch(JSONException e)
    {
        e.printStackTrace();
    }
    Cursor res =database.getAllData();
    if (res.getCount() == 0) {
        //show message
        showMessage("Error", "Nothing found");
        return;
    }

这是我的ParsingData内容

String ParsingDta= "{\"Retailers\":[{\"billedret\":0,\"class\":\"com.enteleki.sfa.botree.RetailerSummary\",\"ctgcode\":\"G003\",\"ctgname\":\"Cosmetic\",\"distcode\":\"DL/0004\",\"mktid\":6,\"rtradd1\":\"D 12/225 Sec 7\",\"rtrclassid\":12,\"rtrcode\":\"RE0012\",\"rtrid\":5,\"rtrname\":\"Aggarwal Cosmetics\",\"rtrphoneno\":\"27049003\",\"srpcde\":\"2\",\"valueclasscode\":\"CL004\",\"valueclassname\":\"D\"}]}";

以下是我的insertData函数的DataBaseHelperClass

 public boolean insertData(String rtrname, String ctgname,String rtrphoneno) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    //contentValues.put(COL_2,rtrphoneno);
    contentValues.put(COL_2,rtrname);
    contentValues.put(COL_3, ctgname);
    contentValues.put(COL_4, rtrphoneno);
    long result = db.insert(TABLE_NAME,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

这一直让我疯狂,帮助将深表赞赏

0 个答案:

没有答案