samedi 2 avril 2016

How to retrieve data using DatabaseHelper classand send data to webservice

I want to retrieve data from DatabaseHelper classand want to send data to PhpMyAdmin. HOw do i do that?here is the code of selecting all data from database. I want to check response for RESULT_OK and then want to send data for PhpMyAdmin.

code to retrieve data

/* Retrive  data from database */
public List<DatabaseModel> getDataFromDB(){
    List<DatabaseModel> modelList = new ArrayList<DatabaseModel>();
    String query = "select * from "+STUDENT_TABLE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()){
        do {
            DatabaseModel model = new DatabaseModel();
            model.setpid(cursor.getString(0));
            model.setname(cursor.getString(1));
            model.setsize(cursor.getString(2));
            model.setcolor(cursor.getString(3));
            model.setqty(cursor.getString(4));
            model.setPrice(cursor.getString(5));
            model.setImage(cursor.getBlob(6));

            modelList.add(model);
        }while (cursor.moveToNext());
    }


    Log.d("student data", modelList.toString());
    db.close();

    return modelList;

}

code for onActivityResult

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // check if the request code is same as what is passed here it is 2
    System.out.println("RESULTCODE--->" + resultCode);
    System.out.println("REQUESTCODE--->" + requestCode);
    System.out.println("RESULT_OK--->" + RESULT_OK);

    if (requestCode == 1) {
        System.out.println("---------INSIDE-------");

        if (data != null) {
            String message = data.getStringExtra("status");
            String[] resKey = data.getStringArrayExtra("responseKeyArray");
            String[] resValue = data.getStringArrayExtra("responseValueArray");

            if(resKey!=null && resValue!=null)
            {
                for(int i=0; i<resKey.length; i++)
                    System.out.println("  "+i+" resKey : "+resKey[i]+" resValue : "+resValue[i]);
                DatabaseHelpher empClick = new DatabaseHelpher(getApplicationContext());
                Cursor c = empClick.getCursorDataFromDB();
                while (c.moveToNext()) {
                    ArrayList<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>();

                    nameValuePairs1.add(new BasicNameValuePair("pid", c.getString(c.getColumnIndex("pid"))));
                    // nameValuePairs1.add(new BasicNameValuePair("itemPrice", c.getString(c.getColumnIndex("price"))));
                    // nameValuePairs1.add(new BasicNameValuePair("itemCount", c.getString(c.getColumnIndex("increment"))));
                    //  nameValuePairs1.add(new BasicNameValuePair("itemCode", c.getString(c.getColumnIndex("item_code"))));
                    Log.i("idddd",c.getString(c.getColumnIndex("pid")));

                    try {
                        HttpClient httpclient1 = new DefaultHttpClient();                   
                        HttpPost httppost1 = new HttpPost("http://ift.tt/1RT4feA");
                        httppost1.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
                        HttpResponse response = httpclient1.execute(httppost1);

                        HttpEntity entity1 = response.getEntity();
                        is = entity1.getContent();
                        Log.e("pass 1", "connection success ");
                    } catch (Exception e) {
                        Log.e("Fail 1", e.toString());
                        internetstringcheck = "Invalid IP Address";


                    }

                    try {
                        BufferedReader reader = new BufferedReader
                                (new InputStreamReader(is, "iso-8859-1"), 8);
                        StringBuilder sb = new StringBuilder();
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                        is.close();
                        result = sb.toString();
                        Log.e("pass 2", "connection success ");
                    } catch (Exception e) {
                        Log.e("Fail 2", e.toString());
                    }

                    try {
                        JSONObject json_data = new JSONObject(result);
                        code = (json_data.getInt("code"));


                        if (code == 1) {
                            Toast.makeText(getBaseContext(), "Inserted Successfully",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getBaseContext(), "Sorry, Try Again",
                                    Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                        Log.e("Fail 3", e.toString());
                    }
                }
            }

            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
            System.out.println("RECEIVED BACK--->" + message);

        }

    }

}

Aucun commentaire:

Enregistrer un commentaire