samedi 23 janvier 2016

saving json data into sqlite

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

}

Aucun commentaire:

Enregistrer un commentaire