如何使用DatabaseHelper类检索数据并将数据发送到webservice

时间:2016-04-02 12:11:08

标签: android sqlite

我想从DatabaseHelper类中检索数据,并希望将数据发送到PhpMyAdmin。我怎么做?这是从数据库中选择所有数据的代码。我想检查RESULT_OK的响应,然后想要发送PhpMyAdmin的数据。

检索数据的代码

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

}

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://192.168.0.127/noddy/test.php");
                        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);

        }

    }

}

0 个答案:

没有答案