0

This getAppData method in Android gives me a blank screen but if I don't convert it to Json Object it gives me this;

["x.apk","xx.apk","xxx.apk"]

but I want the output to be without quotation and square brackets how can achieve this ?

PHP

$fileList = glob('xxxx/xxx/appstore_php/*');
   $files = [];
   foreach($fileList as $filename)
   {
      if ( is_file ( $filename ) )
      {
         $bname = basename ( $filename);
         $files[] = $bname;  
      }
   }

   header('Content-Type: application/json');
   $response = ["files" => $files];
   echo json_encode($response);

Output

   {"files":["x.apk","xx.apk","xxx.apk"]}

Android

    private void getAppData(){
        //Creating a string request
        StringRequest stringRequest = new StringRequest(URLAddress.SHOW_ALL_APK,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        JSONObject json = new JSONObject(response);
                        JSONArray jsonArr = json.getJSONArray("files");
                        for (int i = 0; i < jsonArr.length(); i++) {
                            j = jsonArr.getJSONObject(i);
                            appList.add( j.toString() );
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
        //Creating a request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue
        requestQueue.add(stringRequest);
    }
1
  • 1
    You should use jsonArr.getString(i), because getJSONObject only works with JSON objects, but not JSON strings. Commented Jul 4, 2018 at 15:21

1 Answer 1

1

Try:

for (int i = 0; i < jsonArr.length(); i++) {
   appList.add( jsonArr.getString(i) );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.