0

我已经定义了一个模型类以及其中的一个接口。代码如下:

public class MapModel {
    MapContract.Presenter mPresenter;
    private Location mResLocation;
    private static final String TAG = MapModel.class.getSimpleName();

    public MapModel(MapContract.Presenter presenter) {
        mPresenter = presenter;
    }
    public static WebServiceInterface WebServiceInterface = RetrofitServiceGenerator.create(WebServiceInterface.class);


    public void queryAddress(String address) throws IOException {

        WebServiceInterface .lookupAddress(Constants.GOOGLE_API_KEY, address).enqueue(
                new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {


                        mResLocation = response.body().getGeometry().getLocation();


 Log.i(TAG, "onResponse: " + response.toString());
                            Log.i(TAG, "Location : " + mResLocation.getLat() + " : " + mResLocation.getLat());
                            mPresenter.onSearchSuccess(mResLocation);
                        }



                  @Override
                    public void onFailure(Call<Result> call, Throwable t) {mPresenter.onSearchFailed("Search failed");
                    }
                }
        );
    }
    public interface WebServiceInterface {

        @GET("json")
        Call<Result> lookupAddress(@Query("key") String apiKey, @Query("address") String searchTerm);
    }

}

我的改造发电机如下:

public class RetrofitServiceGenerator {
    public static <S> S create(Class<S> serviceClass) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor)
                .readTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS).build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://maps.googleapis.com/maps/api/geocode/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        return retrofit.create(serviceClass);
    }
}

我遇到的问题是,当我调用该方法时queryAddress,它似乎返回 null。我已经为使用http://www.jsonschema2pojo.org/获得的 json 结果创建了 POJO 对象。您可以在此处查看完整的代码以及 POJO 类类。我得到的错误如下:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Double sematec.mehdi.mymap.webmodels.Location.getLat()' on a null object reference 我认为我的请求是正确的,因为 okhttp 日志显示了正确的 json 响应。所以罪魁祸首在别处。有谁知道我错在哪里?

4

2 回答 2

1

我终于找到了问题的根源。如果您查看网络模型,还有一个 GoogleMapModel 类。调用方法必须返回该类型。接下来,我们将从中提取位置信息。因此,代码变为:

public class MapModel {
    MapContract.Presenter mPresenter;
    private Location mResLocation;
    private static final String TAG = MapModel.class.getSimpleName();

    public MapModel(MapContract.Presenter presenter) {
        mPresenter = presenter;
    }
    public static WebServiceInterface WebServiceInterface = RetrofitServiceGenerator.create(WebServiceInterface.class);


    public void queryAddress(String address) throws IOException {

        WebServiceInterface .lookupAddress(Constants.GOOGLE_API_KEY, address).enqueue(
                new Callback<GoogleMapModel>() {
                    @Override
                    public void onResponse(Call<GoogleMapModel> call, Response<GoogleMapModel> response) {
                        mResLocation = response.body().getResults().get(0).getGeometry().getLocation();
                        mPresenter.onSearchSuccess(mResLocation);
                    }

                    @Override
                    public void onFailure(Call<GoogleMapModel> call, Throwable t) {mPresenter.onSearchFailed("Search failed");
                    }
                }
        );
    }
    public interface WebServiceInterface {

        @GET("json")
        Call<GoogleMapModel> lookupAddress(@Query("key") String apiKey, @Query("address") String searchTerm);
    }

}
于 2018-01-26T07:47:24.683 回答
1

问题是您在从响应中设置对象之前记录了纬度/经度。这条线

Log.i(TAG, "Location : " + mResLocation.getLat() + " : " + mResLocation.getLat());

应该在您从响应中设置对象之后。

所以应该是:

mResLocation = response.body().getGeometry().getLocation();
Log.i(TAG, "Location : " + mResLocation.getLat() + " : " + mResLocation.getLat());
于 2018-01-26T06:59:38.600 回答