节能JSON数据到源码 [英] saving json data into sqlite

查看:167
本文介绍了节能JSON数据到源码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要保存在SQLite的JSON数据。但IAM得到下面的错误。


  

不能内螺纹创建处理程序没有调用
  活套。prepare()。


这是我的code。据shwoing打开数据库/数据库中创建...

  / **
     *异步任务类,通过HTTP调用获得JSON
     * * /
    私有类GetDetails扩展的AsyncTask<太虚,太虚,太虚> {        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
            //显示进度对话框
            pDialog =新ProgressDialog(MainActivity.this);
            pDialog.setMessage(请稍候...);
            pDialog.setCancelable(假);
            pDialog.show();        }    @燮pressLint(NewApi)
    @覆盖
    保护无效doInBackground(虚空......为arg0){
        //创建服务处理程序类实例
        SH的ServiceHandler =新的ServiceHandler();        //创建一个数组来填充微调
        branchlist =新的ArrayList<串GT;();        //制作到URL的请求和响应得到
        字符串jsonStr = sh.makeServiceCall(URL,ServiceHandler.GET);        Log.d(回应:,>中+ jsonStr);//的System.out.println(回应+ jsonStr);        如果(jsonStr!= NULL){            尝试{
                 // jsonString是保存JSON字符串变量
                JSONArray itemArray =新JSONArray(jsonStr);
                的for(int i = 0; I< itemArray.length();我++){
                   值= itemArray.getString(ⅰ);
                    Log.e(JSON,我+=+值);                    dbhelper =新DataBaseHepler(getApplicationContext());
                    sqLiteDatabase = dbhelper.getWritableDatabase();
                    dbhelper.addinnformation(值,sqLiteDatabase);
                    Toast.makeText(getBaseContext(),数据保存,Toast.LENGTH_LONG).show();
                    dbhelper.close();                    branchlist.add(itemArray.getString(I));                }
            }赶上(JSONException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }        }其他{
            Log.e(的ServiceHandler,无法从URL得到任何数据);
        }        返回null;
    }    @覆盖
    保护无效onPostExecute(虚空结果){
        super.onPostExecute(结果);
        //辞退进度对话框
        如果(pDialog.isShowing())
            pDialog.dismiss();
        / **
         *更新解析JSON数据到ListView控件
         * * /        ArrayAdapter<串GT; stringadapter =新ArrayAdapter<串GT;(MainActivity.this,
                android.R.layout.simple_spinner_dropdown_item,
                branchlist);        spinner1.setAdapter(stringadapter);// spinner1
// .setAdapter(新ArrayAdapter<串GT;(MainActivity.this,
// android.R.layout.simple_spinner_dropdown_item,
// branchlist));
    }}


解决方案

此错误是由于吐司 doInBackground(虚空......为arg0 )方法:

  Toast.makeText(getBaseContext(),数据保存,Toast.LENGTH_LONG).show();

显然,Android操作系统不会让比主线程改变UI元素的其他线程。按照此链接以获取更多的细节: https://dzone.com /用品/ android-%E2%80%93多线程的UI

Hi i need to save json data in sqlite. But iam getting following error.

Can't create handler inside thread that has not called Looper.prepare().

This is my code. It is shwoing database open/database created...

/**
     * 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));
    }

}

解决方案

This error is due to Toast inside doInBackground(Void... arg0) method:

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

Clearly the Android OS wont let threads other than the main thread change UI elements. Follow this link for more details on this: https://dzone.com/articles/android-%E2%80%93-multithreading-ui

这篇关于节能JSON数据到源码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆