改造2等待响应

时间:2018-11-09 09:09:13

标签: android asynchronous retrofit2 android-networking

遇到了Retrofit 2问题,不知道它是否与服务器错误的响应处理或我的代码有关。

有用信息

ApiUtils.getUserAPI(getApplicationContext());

以下代码位于getUserApi中,以创建正确的OkHttpClient

 public static UserAPI getUserAPI(Context context) {
    String baseUrl = context.getString(R.string.base_context);
    if (gson == null) {
        gson = new GsonBuilder()
                .setLenient()
                .create();
    }
    if (okHttpClient == null) {
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .followRedirects(true)
                .followSslRedirects(true)
                .addInterceptor(new AddHeadersInterceptor())
                .addInterceptor(new ReceivedCookiesInterceptor())
                .build();
    }
    return new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create(gson)).client(okHttpClient).build().create(UserAPI.class);
}

两个拦截器添加了一些标头和sessionid cookie(我已经添加了标头来模拟真实呼叫

HttpUrl url = chain.request().url();
    Request.Builder builder = chain.request().newBuilder();
    builder.addHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    builder.addHeader("accept-language", "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,es;q=0.6,fr;q=0.5,ar;q=0.4");
    builder.addHeader("content-type", "application/x-www-form-urlencoded");
    builder.addHeader("origin", "https://www.XXX.XX");
    builder.addHeader("referer", url.toString());
    builder.addHeader("User-Agent", USER_AGENT);
    List<String> cookies = SessionData.getInstance().getCookies();
    if (cookies != null && cookies.size() >= 1)
        builder.addHeader("Cookie", cookies.get(0).split(";")[0] + ";");
    return chain.proceed(builder.build());

创建通话

Call<ResponseBody> login = userAPI.login(user.getUserName(), user.getPassword());

回应的处理

login.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                try {
                    Document doc = Jsoup.parse(response.body().string());
                    if (doc.getElementsByClass("flash flash-error") != null && doc.getElementsByClass("flash flash-error").size() > 0) {
                        fullScreenOverlay.setVisibility(View.GONE);
                        Toast.makeText(getContext(), doc.getElementsByClass("flash flash-error").get(0).text(), Toast.LENGTH_LONG).show();
                        onLoginFail();
                    } else {
                       User user = ContentDeserializer.getInstance().parseNewUser(doc);
                        SessionData.getInstance().setUser(user );
                        onLoginDone(user );
                    }

                } catch (IOException e) {
                    Toast.makeText(getContext(), getString(R.string.generic_error), Toast.LENGTH_LONG).show();
                    fullScreenOverlay.setVisibility(View.GONE);

                }
            } else {
                call.cancel();
                Toast.makeText(getContext(), response.errorBody().toString(), Toast.LENGTH_LONG).show();
                fullScreenOverlay.setVisibility(View.GONE);

            }
        }

由于没有公共API来检索代码,因此我必须模拟移动代理调用并解析页面内容以检索数据。

问题

问题在于,如果连接速度较慢(例如,我可以重现强制2G连接)

 if (response.isSuccessful()) {

当我解析使用

找到的div的HTML内容中的数据时,返回true
<span class="bt-label__state" data-button-state="submiting"> **Connecting** <span class="bt-icon bt-icon--loader"></span> </span>

如果我切换到4G或可以正常连接,问题就会消失

似乎未加载我的页面,但在加载页面时进行了改造。这是网页为我提供有效的响应代码(在Chrome开发人员工具中为200或302)的问题,或者我可以做些什么?

0 个答案:

没有答案