0
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

HahaHehe
  • 41
  • 1
  • 1
  • 6

2 Answers2

0

first declare a json array with response text and then make a JSONObject. try this..

   try {
        JSONArray result = new JSONArray(response);
        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();
    }
Kunal Awasthi
  • 310
  • 2
  • 14
0

Did you import/include the class 'Config' file , before accessing it ?

And please put your code in try/catch block and output the exception for more information.

  • Value connection of type java.lang.String cannot be converted to JSONObject – HahaHehe Jul 16 '17 at 06:43
  • Reason is some un-wanted characters was added while compose the String 'response'. please format it properly. Can output the response string ?? will be helpful – Rajesh Kumar Jul 16 '17 at 06:52
  • how do I do that by the way ? I am a beginner in Android Studio and have no idea how to output the response string. Thanks in advance – HahaHehe Jul 16 '17 at 06:57
  • refer to this link [here] https://stackoverflow.com/questions/16780294/how-to-print-to-the-console-in-android-studio – Rajesh Kumar Jul 16 '17 at 07:10
  • {"result":[{"BusinessName":"KachangPuteh","AmountTotal":"100","RequiredTotal":"200","MaxTotal":"500"}]} – HahaHehe Jul 16 '17 at 07:17
  • great! this json string got some hidden values which is causing the issue, this is how it looks {"result":[{"BusinessName":"KachangPuteh","AmountTotal":"100‌​","RequiredTotal":"2‌​00","MaxTotal":"500"‌​}]}, you can validate your json [here](https://jsonformatter.curiousconcept.com/) – Rajesh Kumar Jul 16 '17 at 07:26
  • and how would I solve it? I have tried JSONObject jsonObject = new JSONObject(response); String results= jsonObject.getString(Config.JSON_ARRAY); JSONArray result = new JSONArray(results); – HahaHehe Jul 16 '17 at 07:44
  • quick fix would be to replace ‌ and ​ with empty string and then try to convert to JSON object – Rajesh Kumar Jul 16 '17 at 08:03
  • And how do I do that? Would be helpful if code is given as I do not know how to do it – HahaHehe Jul 16 '17 at 08:09
  • `$str = json_encode(array("result"=>$result)); $str=str_replace('​','',$str); $str=str_replace('‌','',$str); echo $srt;` in you php file before echoing the output – Rajesh Kumar Jul 16 '17 at 08:25
  • any luck @HahaHehe ?? – Rajesh Kumar Jul 17 '17 at 04:42
  • No but I did a different way – HahaHehe Jul 17 '17 at 14:09
  • how, can you post your answer please ? – Rajesh Kumar Jul 17 '17 at 14:10
  • As in instead of using volley i just used http which worked – HahaHehe Jul 17 '17 at 14:44