Reputation: 11
I am creating a class in .java that gets data from mysql. How can I make sure that the data I got from mysql is based on the userID that logged in mobile? Here is my code that run to get all data from 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();
}
}
This is my .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);
?>
Upvotes: 0
Views: 821
Reputation: 1738
Well, that's going to be based on how you have your table set up. If you have a table named dealer and you want an ID associated with it add a 'user_id' column to store the user's id. From there, when the user logs in pass that id to your php script. Then you can use the query
$user_id = strip_tags(stripslashes($_GET['user_id']));
$query="SELECT * FROM dealers where user_id='$user_id'";
to get data associated with that user_id
To make this request in java just pass the user id in the url you are using
result = getAsyncData.execute("http://10.0.2.2/cibi/viewDealer.php?user_id="+varHoldingUserId).get();
Upvotes: 1