java.net.UnknownHostException:无法解析主机“”:没有与主机名关联的地址

时间:2018-12-11 13:04:44

标签: android android-studio flatmap

当我连接到某个WiFi网络时,我试图在服务器上发布一个对象类型为Attendance的帖子。 sendAttendanceListFromCacheObservable方法与此处无关。在flatMap内部调用postAttendance方法时,它总是在onError上输入

消息
  

java.net.UnknownHostException:无法解析主机“”:无地址   与主机名关联。

这是postAttendance方法。

 public void postAttendance(String date, String checkIn, String checkOut, String breakTime, boolean finishActivity) {
    view.showProgress(view.getStringFromResId(R.string.wait));
    Attendance attendance = new Attendance();
    attendance.setDate(date);
    attendance.setCheckInTime(checkIn);
    attendance.setCheckoutTime(checkOut);
    attendance.setBreakTime(DateUtils.getNumberOfMinutesFromHourStringWithDifferentFormat(breakTime));
    //aici
    addSubscription(attendanceManager.sendAttendanceListFromCacheObservable()
            .flatMap(response -> {
                return attendanceManager.postAttendance(attendance);
            })
            .doOnNext(response -> {
                if(response != null) {
                    sessionManager.setGeofenceStatus("");
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .unsubscribeOn(Schedulers.io())
            .doAfterTerminate(() -> {
                view.hideProgress();
            })
            .subscribe(new Subscriber<Attendance>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    Log.e(TAG, "", e);
                    view.handleError(e, true);
                }

                @Override
                public void onNext(Attendance response) {
                    if (finishActivity) {
                        view.finishActivity();
                    } else {
                        view.postAttendanceSuccessfully(response);
                    }
                }
            }));
}

这是给我错误的方法。从未调用.flatMap(isReachable-> {}。

 public Observable<Attendance> postAttendance(Attendance attendance) {
    return Observable.fromCallable(() -> InetAddress.getByName(BuildConfig.PING_URL).isReachable(TIMEOUT))
            .flatMap(isReachable -> {
                if (isReachable) {
                    return apiService.postAttendance(sessionManager.getAccessToken(), attendance)
                            .doOnError(throwable -> FileUtils.writeFile(TAG + " " + throwable.getMessage()))
                            .doOnNext(r -> FileUtils.writeFile(TAG + " attendance successfully sent to server: date: " + attendance.getDate() + " checkin: " + attendance.getCheckInTime() + " checkout" + attendance.getCheckoutTime()))
                            .flatMap(serverAttendance -> getUserMonthlyActivity(attendance.getDate())
                                    .map(userMonthlyActivities -> serverAttendance));
                } else {
                    UserAttendanceDB userAttendanceDB = new UserAttendanceDB();
                    userAttendanceDB = UserAttendanceDB.convertUserAttendanceInUserAttendanceDB(userAttendanceDB, attendance);
                    userAttendanceDao.insert(userAttendanceDB);
                    return Observable.empty();
                }
            });

}

改造

 private boolean configured = false;

private HttpLoggingInterceptor loggingInterceptor;
private CookieInterceptor cookieInterceptor;
private HeadersInterceptor headersInterceptor;

private Retrofit.Builder retrofitBuilder;

protected OkHttpClient.Builder httpClientBuilder;
private OkHttpClient httpClient;

@Inject
public ApiServiceProvider(Retrofit.Builder retrofitBuilder, OkHttpClient.Builder httpClientBuilder,
                          HttpLoggingInterceptor loggingInterceptor, CookieInterceptor cookieInterceptor,
                          HeadersInterceptor headersInterceptor) {

    this.httpClientBuilder = httpClientBuilder;
    this.retrofitBuilder = retrofitBuilder;
    this.loggingInterceptor = loggingInterceptor;
    this.cookieInterceptor = cookieInterceptor;
    this.headersInterceptor = headersInterceptor;
}

private void buildClient() {
    httpClientBuilder.addInterceptor(headersInterceptor);
    httpClientBuilder.addNetworkInterceptor(loggingInterceptor);
    httpClientBuilder.addInterceptor(cookieInterceptor);

    httpClient = httpClientBuilder.build();
}

@Override
public ApiService get() {
    if (!configured || httpClient == null) {
        buildClient();
        configured = true;
    }

    Retrofit retrofit = retrofitBuilder.client(httpClient).build();

    return retrofit.create(ApiService.class);
}

1 个答案:

答案 0 :(得分:0)

TIMEOUT的值是多少?是秒还是毫秒?

 * @param   timeout the time, in milliseconds, before the call aborts
 * @return a {@code boolean} indicating if the address is reachable.
 * @throws IOException if a network error occurs
 * @throws  IllegalArgumentException if {@code timeout} is negative.
 * @since 1.5
 */
public boolean isReachable(int timeout) throws IOException {
    return isReachable(null, 0, timeout);
}

TIMEOUT是通用名称,因此请确保已正确导入。