0

Pretty simple question, I think? How can I create a JSON file and insert (append) the new data I requested (GET) from Volley. So to recap, create file and append it the response I GET. Thanks!

// Formulate the request and handle the response.
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, (String )null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(getActivity(), response.toString() , Toast.LENGTH_LONG).show();
                }

            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), "Error Fetching Data", Toast.LENGTH_LONG).show();
        }
    });

    mRequestQueue.add(jsonObjectRequest);

How I read the file when it exists on the user's phone (Pretty unimportant, but nonetheless gives context)

public String loadJSONFromAsset() {

    String json = null;
    try {
        InputStream is = getActivity().getAssets().open("test12.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        Toast.makeText(getActivity(), "Error Fetching Data", Toast.LENGTH_LONG).show();
        return null;
    }

    return json;

}

Thanks!!

1 Answers1

0

The problem is because your loadJSONFromAsset doesn't make json. It only reads file and puts data into a string. YOu have to convert to json with

final JsonObject savedJson = new JsonObject(json)

then in your volley onResponse you can put them together. see this answer (by someone else) for how to do that : https://stackoverflow.com/a/2403453/267540

Community
  • 1
  • 1
middlestump
  • 1,035
  • 8
  • 22