数据库为空并且请求已完成的异步类错误

时间:2015-06-17 16:49:23

标签: android json database android-asynctask android-volley

AsyncTask有一个JsonArrayRequest课程。在doInBackground方法中,我从服务器接收数据,然后将其粘贴到我的本地SQLite DB。然后我用onPostExecute方法从这个数据库中获取数据。但我收到一个错误,说DB是空的,然后我收到一个通知请求已完成,但我希望它首先将数据放入DB,然后从中读取。我怎么解决这个问题?

private class JsonRequest extends AsyncTask<Void, Void, Void>{
    @Override
    protected Void doInBackground(Void... param) {
        Looper.prepare();
        //Show loading dialog
        pDialog = new ProgressDialog(ProfileActivity.this);
        pDialog.setMessage("Loading...");
        pDialog.show();

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("name", "30");

        // Creating volley request obj
        JsonArrayPostRequest  articleReq = new JsonArrayPostRequest(url_article,
                new Response.Listener<JSONArray>() {

                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        userDB = sqlHelper.getWritableDatabase();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject obj = response.getJSONObject(i);

                                //making short description
                                String description = Html.fromHtml(obj.getString("descriere")).toString();
                                String shortDescription = description.substring(1, 200) + "...";

                              //  Log.d("POSTREQUSET", obj.get("lol").toString());

                                //put new variables in local db
                                ContentValues cv = new ContentValues();
                                cv.put(UserDataHelper.AID, obj.getString("id"));
                                cv.put(UserDataHelper.NAME, obj.getString("nume"));
                                cv.put(UserDataHelper.shortDESCRIPTION, shortDescription);
                                cv.put(UserDataHelper.longDESCRIPTION, obj.getString("descriere"));
                                cv.put(UserDataHelper.PHOTO, "http://mybebe.md/foto/articole/" + obj.getString("foto"));

                                //declare error of local db
                                boolean db_error = false;

                                //declare cursor for parsing local db
                                Cursor cursor = userDB.query(UserDataHelper.TABLE_NAME, new String[]{
                                                UserDataHelper.AID, UserDataHelper.NAME}, null,
                                        null,
                                        null,
                                        null,
                                        null
                                );

                                //checking for similar items in local db
                                while (cursor.moveToNext()) {
                                    String name = cursor.getString(cursor
                                            .getColumnIndex(UserDataHelper.NAME));
                                    if (name.equals(cv.get(UserDataHelper.NAME))) {
                                        db_error = true;
                                    }
                                }
                                cursor.close();

                                //put in local db if there is no similiar item
                                if (!db_error) userDB.insert(UserDataHelper.TABLE_NAME, null, cv);

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

                        }
                        //notify changes in article list
                        adapter.notifyDataSetChanged();
                        userDB.close();
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        }, params);
        AppController.getInstance().addToRequestQueue(articleReq);

        hidePDialog();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
       LayoutInflater inflater = LayoutInflater.from(ProfileActivity.this);
       View v = inflater.inflate(R.layout.button_load_more, null);

       toTakeArticlesFromDB(v);
       onItemClick();
    }
}

这是我的'TakefromDB'方法:

private void toTakeArticlesFromDB (View view){
    userDB = sqlHelper.getWritableDatabase();

    ListView listView = (ListView) findViewById(R.id.list);
    articleList.clear();


    adapter = new CustomListAdapter(ProfileActivity.this, articleList);
    listView.setAdapter(adapter);


    Cursor cursor = userDB.query(UserDataHelper.TABLE_NAME, new String[]{
                    UserDataHelper.AID, UserDataHelper.NAME, UserDataHelper.shortDESCRIPTION, UserDataHelper.longDESCRIPTION, UserDataHelper.PHOTO}, null,
            null,
            null,
            null,
            null
    );
    cursor.moveToLast();

    do {
        try{
            Article article = new Article();

            Log.i("Article_DB indexs", cursor.getString(cursor.getColumnIndex(UserDataHelper.AID)));

            article.setTitle(cursor.getString(cursor.getColumnIndex(UserDataHelper.NAME)));
            article.setDescription(cursor.getString(cursor.getColumnIndex(UserDataHelper.shortDESCRIPTION)));
            article.setThumbnailUrl(cursor.getString(cursor.getColumnIndex(UserDataHelper.PHOTO)));
            article.setIsOpen(false);

            articleList.add(article);
        }catch (CursorIndexOutOfBoundsException e){
            e.printStackTrace();
            refreshButtonBig = (ImageButton) findViewById(R.id.imageButtonRefresh);
            refreshButtonBig.setVisibility(View.VISIBLE);
            refreshButtonBig.requestFocus();
        }

    } while (cursor.moveToPrevious());


    cursor.close();
    userDB.close();

    if(listView.getFooterViewsCount() == 0) {
        listView.addFooterView(view);
    }

}

0 个答案:

没有答案