【问题标题】:saving json data into sqlite将 json 数据保存到 sqlite
【发布时间】:2016-01-23 17:46:25
【问题描述】:

您好,我需要将 json 数据保存在 sqlite 中。但我得到以下错误。

无法在未调用的线程内创建处理程序 Looper.prepare().

这是我的代码。它正在显示数据库打开/数据库创建...

/**
     * Async task class to get json by making HTTP call
     * */
    private class GetDetails extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

    @SuppressLint("NewApi")
    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Create an array to populate the spinner 
        branchlist = new ArrayList<String>();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

//        System.out.println("response"+jsonStr);

        if (jsonStr != null) {

            try {
                 // jsonString is a string variable that holds the JSON 
                JSONArray itemArray=new JSONArray(jsonStr);
                for (int i = 0; i < itemArray.length(); i++) {
                   value=itemArray.getString(i);
                    Log.e("json", i+"="+value);

                    dbhelper=new  DataBaseHepler(getApplicationContext());
                    sqLiteDatabase=dbhelper.getWritableDatabase();
                    dbhelper.addinnformation(value,sqLiteDatabase);
                    Toast.makeText(getBaseContext(),"Data saved",Toast.LENGTH_LONG).show();
                    dbhelper.close();

                    branchlist.add(itemArray.getString(i));

                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */

        ArrayAdapter<String> stringadapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_dropdown_item,
                branchlist);

        spinner1.setAdapter(stringadapter);

//      spinner1
//      .setAdapter(new ArrayAdapter<String>(MainActivity.this,
//              android.R.layout.simple_spinner_dropdown_item,
//              branchlist));
    }

}

【问题讨论】:

  • Toast 消息直接在后台执行是不可能的。应该使用runonuithread。这是因为 doinbackground 在后台线程上

标签: android sqlite


【解决方案1】:

这个错误是由于Toast里面的doInBackground(Void... arg0)方法:

Toast.makeText(getBaseContext(),"Data saved",Toast.LENGTH_LONG).show();

显然,Android 操作系统不会让主线程以外的线程更改 UI 元素。点击此链接了解更多详情:https://dzone.com/articles/android-%E2%80%93-multithreading-ui

【讨论】:

    猜你喜欢
    • 2019-11-16
    • 1970-01-01
    • 2016-07-30
    • 2011-09-11
    • 1970-01-01
    • 2016-09-13
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多