改造调用中出现意外的IndexOutOfBoundsException

时间:2019-07-07 15:18:39

标签: android retrofit2

我在我的应用中通过Retrofit轻松调用了Google Map API 我在GooglePlay控制台和一部手机中看到了错误,但无法在模拟器和其他手机上重现该错误-一切正常。 这是java.lang.IndexOutOfBoundsException

只有在应用保持几天或几周不变的情况下,才会出现类似错误的提示 而且PlayConsole表示该问题在低于7.0的Android版本中不会出现

请在下面显示代码 当“ try-catch”循环开始时,Stacktrace指示该行上的错误。我以为我只是有错误,因为当没有数据响应时,我试图“ get(0)”。但是我已经重现了这种行为,并且我的“ catch”块可以完美地解决此问题,该应用并未崩溃

毕竟堆栈跟踪为什么指示字符串76而不是实际发生调用的字符串77?

这是有错误的方法

    Call<Geocod> response = geocoding_interfaces.CoordinatesFactory.getInstance().getCoordinates(coordinates, private_key);

        try {      //HERE IS STRING 76 
            incomelocation = response.execute().body().getResults().get(0).getGeometry().getLocation();
            askinglocation = from_loc_to_string(incomelocation);
        } catch (IOException | IndexOutOfBoundsException e) {
            askinglocation = locations;
        }

这是回调

   @Override
        public void onLoadFinished( Loader<List<Datum>> loader, List<Datum> data) {
            int id = loader.getId();
            if (id == R.id.weather_loader_id) {
                if (data != null) {
                      // here some methods to work with data
                } else
                {
                     // here some methods to work errors out 
                }

这是主要的改装界面

import android.support.annotation.NonNull;
import android.util.Log;

import java.util.concurrent.TimeUnit;

import dev.eyesless.simple_weather_for_fishermans.BuildConfig;
import dev.eyesless.simple_weather_for_fishermans.geocoding_responce_classes.Geocod;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface geocoding_interfaces {

    String BASE_URL = "https://maps.googleapis.com/maps/api/";

    @GET("geocode/json")
    Call<Geocod> getCoordinates(@Query("address") String order, @Query("key") String key);

    class CoordinatesFactory {

        private static geocoding_interfaces service;

        public static geocoding_interfaces getInstance() {

            if (service == null) {

                HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override public void log(@NonNull String message) {
                        Log.e("MY_TAG", "OkHttp: " + message);
                    }
                });

                logging.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BASIC : HttpLoggingInterceptor.Level.NONE);

                OkHttpClient client = new OkHttpClient.Builder()
                        .addInterceptor(logging) // TODO: 15.10.2017 remoove before production
                        .retryOnConnectionFailure(false)
                        .connectTimeout(5, TimeUnit.SECONDS)
                        .readTimeout(1, TimeUnit.SECONDS)
                        .build();

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(client)
                        .build();

                service = retrofit.create(geocoding_interfaces.class);

                return service;
            }

            else {
                return service;
            }
        }
    }
}

Stacktrace:

java.lang.RuntimeException: 
  at android.support.v4.content.ModernAsyncTask$3.done (ModernAsyncTask.java:161)
  at java.util.concurrent.FutureTask.finishCompletion (FutureTask.java:383)
  at java.util.concurrent.FutureTask.setException (FutureTask.java:252)
  at java.util.concurrent.FutureTask.run (FutureTask.java:271)
  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1162)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:636)
  at java.lang.Thread.run (Thread.java:784)
Caused by: java.lang.IndexOutOfBoundsException: 
  at java.util.ArrayList.get (ArrayList.java:437)
  at dev.eyesless.simple_weather_for_fishermans.repository.WeatherLoader.loadInBackground (WeatherLoader.java:76)
  at dev.eyesless.simple_weather_for_fishermans.repository.WeatherLoader.loadInBackground (WeatherLoader.java:21)
  at android.support.v4.content.AsyncTaskLoader.onLoadInBackground (AsyncTaskLoader.java:302)
  at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground (AsyncTaskLoader.java:57)
  at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground (AsyncTaskLoader.java:45)
  at android.support.v4.content.ModernAsyncTask$2.call (ModernAsyncTask.java:138)
  at java.util.concurrent.FutureTask.run (FutureTask.java:266)

0 个答案:

没有答案