Android:如何使用参数从mysql获取数据到sqlite

时间:2014-09-26 05:53:28

标签: php android mysql sqlite

我在.java中创建一个从mysql获取数据的类。 我怎样才能确保从mysql获取的数据是基于登录移动设备的用户ID? 这是我的代码,用于从mysql获取所有数据:

package a.a;
    import java.util.concurrent.ExecutionException;
    import org.json.JSONArray;
    import org.json.JSONObject;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.text.format.Time;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class Update_Rolling extends Activity
    {
        Database b = new Database(this);
        Button yes, no;
        String result;
        String dealID, dealName, dealOwner, dealWil, dealTelp, dealKtgr, dealSales;
        String Status1, userName;
        int prDate, prMonth, prYear;

        // -------- INTERNET --------------
        // flag for Internet connection status
        Boolean isInternetPresent = false;

        // Connection detector class
        ConnectionDetector cd;

        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
        setContentView(R.layout.empty);

        b.open();
        yes = (Button)findViewById(R.id.yes);
        no = (Button)findViewById(R.id.no);

        //nerima intent
                Status1 = getIntent().getExtras().getString("status");
                userName = getIntent().getExtras().getString("NPK");

        cd = new ConnectionDetector(getApplicationContext());

        // get Internet status
        isInternetPresent = cd.isConnectingToInternet();


        yes.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // get Internet status
                isInternetPresent = cd.isConnectingToInternet();

                // check for Internet status
                if (isInternetPresent) {
                //function update
                    b.deleteDealerList();
                    b.createListDealer();
                    getWebRolling();

                showAlertDialog(Update_Rolling.this, "Internet Connection",
                            "Data Updated", true);

                } else {
                    // Internet connection is not present
                    showAlertDialog(Update_Rolling.this, "No Internet Connection", 
                            "You don't have internet connection.", false);
                }
            }
        });

        no.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent aaa = new Intent(Update_Rolling.this, Homescreen.class);
                aaa.putExtra("status", Status1);
                aaa.putExtra("NPK", userName);
                startActivity(aaa);
            }
        });

    }

    public void getWebRolling(){
        GetAsynctaskData getAsyncData = new GetAsynctaskData();
        try 
        {
        result = getAsyncData.execute("http://10.0.2.2/cibi/viewDealer.php").get();

        } catch (InterruptedException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        insertRoll(result);
    }

    private void insertRoll(String abc) {
        try {
            JSONArray json = new JSONArray(abc);
            for(int i = 0; i < json.length(); i++)
            {
                JSONObject json_obj = json.getJSONObject(i);            
                //ambil field data dari XAMPP di .getString
                dealID = json_obj.getString("CustCode");
                dealName = json_obj.getString("Nama");
                dealWil = json_obj.getString("Kota");
                dealSales = json_obj.getString("Salesman");

                String noSales = dealSales.toString();
                String noToko = dealID.toString();

                Time today = new Time(Time.getCurrentTimezone());
                today.setToNow();
                prDate = today.monthDay;
                prMonth = today.month+1;
                prYear = today.year;

                String LDno = noSales+""+prYear+""+prMonth+""+noToko+"";

                Toast.makeText(Update_Rolling.this, LDno, Toast.LENGTH_LONG).show();

                //ini cuman insert buat pertama kali
                b.insertDealABC(dealID, dealName, dealWil);
                b.insertLD(LDno, dealID, dealSales);

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Toast.makeText(Update_Rolling.this, "update database gagal", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }



    public void showAlertDialog(Context context, String title, String message, Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);

        // Setting alert dialog icon
        //alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent aaa = new Intent(Update_Rolling.this, Homescreen.class);
                aaa.putExtra("status", Status1);
                aaa.putExtra("NPK", userName);
                startActivity(aaa);
            }
        });
        alertDialog.show();
    }
}

这是我的.php: viewDealer.php

 <?php
    include("connect.php");
    $query = "SELECT * FROM dealer";
    $result = mysql_query($query);
        while($data = mysql_fetch_array($result))
        {
        $rows[] = $data;

        }
        echo json_encode($rows);
    ?>

1 个答案:

答案 0 :(得分:1)

那么,这将取决于你如何设置表格。如果您有一个名为dealer的表,并且您想要一个与之关联的ID,请添加一个'user_id'列来存储用户的ID。从那里,当用户登录时将该id传递给您的php脚本。然后您可以使用查询

$user_id = strip_tags(stripslashes($_GET['user_id']));
$query="SELECT * FROM dealers where user_id='$user_id'";

获取与该user_id相关的数据

要在java中发出此请求,只需在您正在使用的网址中传递用户ID

result = getAsyncData.execute("http://10.0.2.2/cibi/viewDealer.php?user_id="+varHoldingUserId).get();