0

I am struggling my way through my first android application which is processing weather satellite data. I am using JSON to retrieve times for tile images from a remote server. Due to my lack of experience with java, I cannot get the jResponse so other portions of the application can use the data. Is there a way to get the jResponse (JSONArray) out of the on response method? When I try and change the method to return a JSONArray it undoes the override. I am quite stuck and would really appreciate any help.

    public  JSONArray getJson(final int i,String sectorName){

    times = new JSONArray();

    String url = "https://storage.googleapis.com/satsquatch-tiles-dev/GOES16_"+String.format("%02d",i)+"_"+sectorName+".json";

    final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("test", "Success");
                    try {
                        JSONArray jResponse = response.getJSONArray("times");

                        for (int count = 0; count < jResponse.length(); count++){
                            Log.d("Array_times",jResponse.get(count).toString());

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("test", "JSON Request Failure Check URL!");


                }
            });
    //add request to queue
    queue.add(jsonObjectRequest);


    return times;

1 Answers1

0

If you want to use data after the whole parsing is done you can go with BroadCastReceivers.
This problem can be broken down into two steps.

  1. Notify that the parsing is done - By sending broadcast

  2. Receive messages using BroadcastReceivers.

    public void onResponse(JSONObject response) {
    
         Log.d("test", "Success");
    
         try {
    
                 JSONArray jResponse = response.getJSONArray("times");
    
                 for (int count = 0; count < jResponse.length(); count++){
                         Log.d("Array_times",jResponse.get(count).toString());
    
                 }
    
                 notify(JsonArray jsonArray);   /// this is the method
    
          } catch (JSONException e) {
                 e.printStackTrace();
          }
    
      }
    

notify() method

public void notify(JsonArray jsonArray){
    Intent i = new Intent();
    i.setAction("Some key");
    i.putExtra("Some key to retrive your array",jsonArray);
    sendBroadcast(i);
}

Receive Broadcast

First register your Receiver like this

BroadcastReceiver receiver = new YourReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(OBJECTMAPPED);
registerReceiver(receiver, filter);

You can receive your JsonArray inside onReceive() method of receiver which would like this

private class YourReceiver extends BroadcastReceiver
{

    private Context context;

    @Override
    public void onReceive(Context context,Intent intent)
    {
        if(intent.getAction().equalsIgnoreCase("Some key"))
        {
            JSONArray jsonArray = (JSONArray)i.get()
            //Do all the processing you want
        }
   } 

}

EDIT:

I'm not sure if you can pass JSONArray inside an intent like that. but it gives you a rough idea of how to deal with such problems. Maybe you can look into this thread to pass JSONArray in an intent

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88