0

I'm attempting to implement a callback for Android Java JsonObjectRequests. I've found dozens of posts showing code that is nearly verbatim. My problem is that the callback is null. See the two inline comments.

public interface JsonObjectCallbackInterface {
    void onSuccess(JSONObject result);
}
class DispatchBuddyBase {
    //...

    public void addGmapMarker(String inAddress) {
        final String address = DBB.prepareAddress(inAddress);
        DatabaseReference ref = DBB.getTopPathRef("/geocodedAddresses");

        ref.orderByChild("address")
                .equalTo(address)
                .addListenerForSingleValueEvent(
                new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.i(TAG, address+" stored: "+dataSnapshot.exists());
                if (!dataSnapshot.exists()) {
                    DBB.getLatLng(
                           "17 River Rd Meriden CT 06451",
                            new JsonObjectCallbackInterface() {
                         //      ^--- why is this passing a **null?**

                        @Override
                        public void onSuccess(JSONObject response) {
                            Log.i(TAG, "got a json body:"+response.toString());
                            addGmapMarker(response);
                        }
                    });
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
}
public class DispatchesActivity extends AppCompatActivity implements
    OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    ResultCallback<LocationSettingsResult> {
    //...

    public void getLatLng(final String address,
                          final JsonObjectCallbackInterface callback) {
        // why does this get a **null** callback? -------------^

        Log.e(TAG, "callback is: "+callback);
        String url = "https://maps.googleapis.com/maps/api/geocode/json?address="
                + address;

        JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.e(TAG,"Response: " + response.toString());
                        DatabaseReference ref = getTopPathRef("/geocodedAddresses").push();

                        Map<String, Object> u = new HashMap<>();
                        u.put("address", address);
                        u.put("geocoded", response.toString());

                        ref.updateChildren(u, new DatabaseReference.CompletionListener() {
                            @Override
                            public void onComplete(DatabaseError databaseError,
                                                   DatabaseReference databaseReference) {
                                if (databaseError != null) {
                                    Log.e(TAG,"Geocoded address could not be saved "
                                            + databaseError.getMessage());
                                } else {
                                    Log.e(TAG,"Geocoded address saved successfully.");
                                }
                            }
                        });
                        Log.e(TAG, "callback2 is: "+callback);
                        callback.onSuccess(response);
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                    }
                });

        DBVolley.getInstance(context)
                .addToRequestQueue(jsObjRequest);
    }
}

To be clear, I did once note that callback was not null just once when I was testing. Repeat runs have not produced a non-null callback since then. I'm only 8 days into Java, so please pardon if I've done something naive.

The DispatchBuddyBase singleton is instanced in the main activity just once, and DBVolley likewise. All additional references obtain the same via DispatchBuddyBase.getInstance(). I haven't had any problems with anonymous classes elsewhere.

The Volley code is bare bones and fires off requests just fine, the JsonObjectRequest successfully gets everything I expect from Google. Everything else is working swell except for this callback.

What do I need to fix to correctly pass this callback?

FirefighterBlu3
  • 399
  • 5
  • 11

1 Answers1

0

Ok, marking this solved. Occam's razor. I unintentionally hit ctrl-z too much and uncommented in another section of code that was calling with a literal null parameter for the callback.

The above code works perfectly.

FirefighterBlu3
  • 399
  • 5
  • 11