简体   繁体   中英

Insert retrofit array data into sqlite

I have manage to retrieve data from the server using retrofit, after that i would like to insert it into sqlite database, however there's some error. what am i doing that is wrong? because i set it into the textview and it shows on the output. why is it return as null object?

我收到的错误

My retrofit part of code

    private void loadJSON() {
    Intent intent = getIntent();
    final String getpaspot = intent.getStringExtra("pasport");

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("XXXXXXXXXXXXXXXX")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RequestInterface request = retrofit.create(RequestInterface.class);
    Call<JSONResponse> call = request.getJSON(getpaspot);
    call.enqueue(new Callback<JSONResponse>() {
        @Override
        public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {

            if(response.isSuccessful()) {
                JSONResponse jsonResponse = response.body();
                jemaah = new ArrayList<>(Arrays.asList(jsonResponse.getJemaah()));
                adapter = new JemaahAdapter(jemaah);
                recyclerView.setAdapter(adapter);

                //List<Jemaah> jemaahs = jsonResponse.getJemaah();
                for (Jemaah j : jemaah){
                    if (j.getManifest1_no_paspot().equals(getpaspot)){
                        reqjemaah = j;

                        String namaJ = reqjemaah.getManifest1_nama().trim();
                        String paspotJ = reqjemaah.getManifest1_no_paspot().trim();
                        String akaunJ = reqjemaah.getManifest1_no_akaun().trim();

                        textView1.setText(namaJ);
                        textView2.setText(paspotJ);
                        textView3.setText(akaunJ);

                        myDb.saveUser(akaunJ, paspotJ, namaJ);
                    }
                }

            }
        }

        @Override
        public void onFailure(Call<JSONResponse> call, Throwable t) {
            Log.d("Error",t.getMessage());
        }
    });
}

SQLiteHandler

    public void saveUser(String manifest1_no_akaun, String manifest1_no_paspot, String manifest1_nama) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_NOAKAUN, manifest1_no_akaun );
    values.put(KEY_NOPASPOT, manifest1_no_paspot );
    values.put(KEY_NAMA, manifest1_nama );

    db.insert(TABLE_USER,null, values);
    db.close();
}

You need to declare & initialise SQLiteHandler class object before using it.

Something like,

SQLiteHandler myDb = new SQLiteHandler(); //add this line
myDb.saveUser(akaunJ, paspotJ, namaJ);

Use Realm for storing the data in the database. It can store object of class without declaring its database structure. It is more convenient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM