传递给另一个函数时,Java匿名类(实现接口)回调为null

时间:2018-02-22 09:22:41

标签: java interface callback jsonobjectrequest geocoder

我正在尝试为Android Java JsonObjectRequests实现回调。我发现几十个帖子显示几乎是逐字的代码。我的问题是callback为空。请参阅两个内联注释。

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);
    }
}

为了清楚起见,我曾经做过一次注意,当我测试时callback不是一次。从那时起,重复运行没有产生非空回调。我只用了8天的Java,所以如果我做了一些天真的事情,请原谅。

DispatchBuddyBase单例只在主活动中实例一次,同样DBVolley。所有其他参考文献都通过DispatchBuddyBase.getInstance()获得相同的参考资料。我在其他地方的匿名课程没有任何问题。

Volley代码很简单,只是很好地解决了请求,JsonObjectRequest成功地获得了我对Google的所有期望。除了这个回调之外,其他所有东西都在膨胀。

正确传递此回调需要修复哪些内容?

1 个答案:

答案 0 :(得分:0)

好的,标记为已解决。奥卡姆剃刀。我无意中点击了ctrl-z太多,并且在另一个用回调的文字空参数调用的代码段中取消注释。

以上代码完美无缺。