jeudi 24 septembre 2015

Send SQLite Database data send to XAMPP Server

I try send SQLite data to XAMPP Server.It works but I face one problem. I Used while loop to send all table data to server.Problem id that It send last row data for every while loop excution. For Example if table have 10 record and Last record is 2 then while loop send 10 time 2 to sever.

public class MainActivity extends Activity implements OnClickListener {
Button getdata, cus_list, update_pay;
List<NameValuePair> params;
ProgressBar pbar;
String url = "http://ift.tt/1KD6fyA?       draw=1&opId=19&start=0&length=-1&val=";
String urlPayment = "http://ift.tt/1NVxvOF";
Connect cn = new Connect();
String getPassword, getUsername, checked = null, opid, op_name,
        op_username, op_address, op_toll, op_contact, saveOpid, savename,
        op_company, saveaddress;
DatabaseConnector dbConnector = new DatabaseConnector(MainActivity.this);
private static final String AMOUNT = "amt";
private static final String CONNO = "conno";

Cursor GET;
int j;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    pbar = (ProgressBar) findViewById(R.id.progressBar1);
    getdata = (Button) findViewById(R.id.b_getdata);
    cus_list = (Button) findViewById(R.id.b_cus_list);
    update_pay = (Button) findViewById(R.id.b_updt_pay);
    update_pay.setOnClickListener(this);
    cus_list.setOnClickListener(this);
    getdata.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.b_getdata:
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "operator"));
        params.add(new BasicNameValuePair("username", "test"));
        params.add(new BasicNameValuePair("password", "7275460750"));

        if (isonline()) {
            new ReadJSONResult().execute(url);
        } else {
            Toast.makeText(getBaseContext(), "connection failed!",
                    Toast.LENGTH_SHORT).show();
        }
        break;
    case R.id.b_cus_list:
        Intent i = new Intent(getApplicationContext(), CustomerList.class);
        startActivity(i);

        break;

    case R.id.b_updt_pay:
        j = 1;

        /* Call GetPayData() Async Methode  */
        new GetPayData().execute((Object[]) null);
        break;
    }
}


private class GetPayData extends AsyncTask<Object, Object, Cursor> {
    DatabaseConnector dbConnector = new DatabaseConnector(MainActivity.this);
    @Override
    protected Cursor doInBackground(Object... params) {
        // Open the database
        dbConnector.open();
        return dbConnector.ListAllPayData();
    }

    @Override
    protected void onPostExecute(Cursor result) {
        // Cusor Value
        GET = result;

        //Read SQLite Data ( payment Table)
        GET.moveToFirst();

        /* This is code Send DATA to Server */

        while (!GET.isAfterLast()) {

            // Store SQL data to params ArrayList
            params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("conNO", result
                    .getString(result.getColumnIndex(CONNO))));
            params.add(new BasicNameValuePair("amt", result
                    .getString(result.getColumnIndex(AMOUNT))));

            // Check Intenet Connection
            if (isonline()) {

                // send data to sever usinf Asunc Task

                new ReadJSONResult().execute(urlPayment);

                Toast.makeText(getBaseContext(), params.toString(),
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getBaseContext(), "connection failed!",
                        Toast.LENGTH_SHORT).show();
            }

            GET.moveToNext();
        }

        dbConnector.close();
    }
}

@SuppressLint("NewApi")
class ReadJSONResult extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        // TODO Auto-generated method stub
        String get = null;
        try {
            get = cn.readJSONFeed(urls[0], params);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return get;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub

        pbar.setVisibility(View.VISIBLE);

    }

    @SuppressLint("NewApi")
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        Dialog d = new Dialog(MainActivity.this);
        TextView tv = new TextView(MainActivity.this);
        tv.setText(result.toString());
        d.setContentView(tv);
        d.show();

        if (result != null) {

            try {
                JSONObject jsonObject = new JSONObject(result);
                if (j == 0) {
                    if ("1".equals(jsonObject.getString("draw"))) {

                        JSONArray jsonArray = jsonObject
                                .optJSONArray("data");

                        for (int i = 0; i < jsonArray.length(); i++) {

                            JSONArray ar = jsonArray.getJSONArray(i);
                            String con = ar.get(0).toString();
                            String name = ar.get(1).toString();
                            String vc_no = ar.get(2).toString();
                            // String address = ar.get(4).toString();
                            // String phone_no = ar.get(5).toString();
                            DatabaseConnector dbConnector = new DatabaseConnector(
                                    MainActivity.this);
                            dbConnector.InsertCustomerData(con, name);
                            Toast.makeText(getApplicationContext(),
                                    "sucess", Toast.LENGTH_LONG).show();

                        }

                        pbar.setVisibility(View.GONE);
                    } else {
                        Toast.makeText(getBaseContext(),
                                "Account Not Found \n Please try again.",
                                Toast.LENGTH_LONG).show();
                        pbar.setVisibility(View.GONE);
                    }
                } else if (j == 1) {
                    if ("1".equals(jsonObject.getString("success"))) {
                        Toast.makeText(getBaseContext(), "Succes",
                                Toast.LENGTH_LONG).show();

                    }
                }
            } catch (Exception e) {
                Log.d("Ex", e.getLocalizedMessage());
                Toast.makeText(getBaseContext(),
                        "onPost == " + e.toString(), Toast.LENGTH_LONG)
                        .show();
                pbar.setVisibility(View.GONE);
            }

        } else {
            Toast.makeText(getBaseContext(),
                    "Bad Connection Please try again later",
                    Toast.LENGTH_LONG).show();
            pbar.setVisibility(View.INVISIBLE);
        }
    }
}

public Boolean isonline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()
            && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected()) {
        return true;
    }
    return false;

}

}

Aucun commentaire:

Enregistrer un commentaire