I want to know if I am right for retrieving datas from an SQL data base. I have an Android application which use the getCategories method for asking a PHP page on a web server. The web page retrieve datas from a SQL database and send them, with JSON format, to my Android application which display it.
I have made this architecture, a Java code which call a PHP page for retrieving data in a SQL data base. I want to know if I am righ or if there is a better way to retrieve my datas.
My java method for asking the web page :
String webAddress = "www.mywebsite.com";
public static void getCategories(final Context context,
final String catForeignKeyCountry,
final List<HashMap<String,String>> categories){
class GetEmployee extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(context,"Fetching...","Wait...",false,false);
}
@Override
protected String doInBackground(Void... params) {
RequestHandler requestHandler = new RequestHandler();
String result = requestHandler.sendGetRequestParam(webAddress+"/getAllCat.php?cat_fk_country=", "1");
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArrayResult = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
for(int i = 0; i < jsonArrayResult.length(); i++){
JSONObject jo = jsonArrayResult.getJSONObject(i);
String id = jo.getString(Config.KEY_CAT_ID);
String name = jo.getString(Config.KEY_CAT_NAME);
HashMap<String,String> category = new HashMap<>();
category.put(Config.KEY_CAT_NAME,id);
category.put(Config.KEY_CAT_NAME,name);
categories.add(category);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
GetEmployee getEmployee = new GetEmployee();
getEmployee.execute();
}
My PHP page for asking the database
//Getting the key of the country
$cat_fk_country = $_GET['cat_fk_country'];
//Importing Database Script
require_once('dbConnect.php');
//Creating sql query
$sql_parti = "SELECT * FROM gc_category
WHERE cat_fk_country=$cat_fk_country";
//getting result
$result_parti = mysqli_query($con,$sql_parti);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($result_parti)){
//Pushing name and id in the blank array created
array_push($result,array("cat_id"=>$row['cat_id'],
"cat_fk_country"=>$row['cat_fk_country'],
"cat_number"=>$row['cat_number'],
"cat_name"=>$row['cat_name']));
}
$empty = empty($result) ;
if($empty == 1){
$sql_parti = "SELECT * FROM gc_category
WHERE cat_fk_country=12";
//getting result
$result_parti = mysqli_query($con,$sql_parti);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($result_parti)){
//Pushing name and id in the blank array created
array_push($result,array("cat_id"=>$row['cat_id'],
"cat_fk_country"=>$row['cat_fk_country'],
"cat_number"=>$row['cat_number'],
"cat_name"=>$row['cat_name']));
}
echo json_encode(array('result'=>$result));
}else{
echo json_encode(array('result'=>$result));
}
mysqli_close($con);