public class Config {
public static final String DATA_URL = "http://192.168.1.2/retrieveone.php?id=";
public static final String KEY_NAME = "BusinessName";
public static final String KEY_AmountTotal = "AmountTotal";
public static final String KEY_RT = "RequiredTotal";
public static final String KEY_MT = "MaxTotal";
public static final String JSON_ARRAY = "result";
}
This is my class file .
private void getData(){
String id = TempItem.toString().trim();
if (id.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
String url = Config.DATA_URL+TempItem.toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.GET,url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(KPDetails.this,error.toString(),Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
int socketTimeout =50000;//5 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String name="";
String AmountTotal="";
String RequiredTotal = "";
String MaxTotal = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject stallsData = result.getJSONObject(0);
name = stallsData.getString(Config.KEY_NAME);
AmountTotal = stallsData.getString(Config.KEY_AmountTotal);
MaxTotal = stallsData.getString(Config.KEY_MT);
RequiredTotal = stallsData.getString(Config.KEY_RT);
} catch (JSONException e) {
e.printStackTrace();
}
Stall.setText("Name:\t"+name+"\nAmountTotal:\t" +AmountTotal+ "\nMaxTotal:\t"+ MaxTotal);
}
This is my constructor to get data from mysql daatabase. However, I managed to get the setText but not the value of the strings in config.
This is my php file.
?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
require_once('conn.php');
$sql = "SELECT * FROM business WHERE BusinessID='".$id."'";
$r = mysqli_query($conn,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"BusinessName"=>$res['BusinessName'],
"AmountTotal"=>$res['AmountTotal'],
"RequiredTotal"=>$res['RequiredTotal'],
"MaxTotal"=>$res['MaxTotal']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($conn);
}
"http://localhost/retrieveone.php?id=1" works perfectly fine but the value just does not show up in my setText. I am a beginner in Android Studio. Any help is appreciated. Thanks in advance.
Edit:
This is my error . Value connection of type java.lang.String cannot be converted to JSONObject