【问题标题】:Retrofit: Blank screen appears in android studio at irregular intervals getting java.net.SocketTimeoutException改造:android studio中不定期出现空白屏幕获取java.net.SocketTimeoutException
【发布时间】:2023-03-09 22:26:01
【问题描述】:

我正在使用改造进行解析。我不确定发生了什么,但是从服务器获取数据时突然出现空白屏幕。而且它不规则,有时会出现或有时不会出现。而且不仅限于单个页面,我的意思是它发生在我使用服务的任何活动中。并且在按一次或两次后退按钮后,会出现列表。 调试后我发现它继续 onFailure 改造方法并给出了一个 java.net.SocketTimeoutException 有谁知道原因吗?您的善意支持将不胜感激。 这是我的代码:

GridView mLvCategories;
CustomCategoriesAdapter adapter;
private GetCustomCategoriesModel[] mArray;
ImageView viewImage;
private String[] navMenuTitles;
private TypedArray navMenuIcons;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_professional_list);
    initializeWidgets();
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); // load
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);// load icons from
    set(navMenuTitles, navMenuIcons);
    callToGetAllCategories();

}

private void initializeWidgets() {
    mLvCategories=(GridView)findViewById(R.id.grid_categories);
}

private void callToGetAllCategories() {
    final ProgressDialog dialog = ProgressDialog.show(ProfessionalListActivity.this, "", "Please wait...");
    RestClient.GitApiInterface service = RestClient.getClient();
    String url="http://scorpioinfotech.net/demo/api/get_categories";
    Call<GetCustomCategoriesModel[]> call = service.hitGetApi(url);
    call.enqueue(new Callback<GetCustomCategoriesModel[]>() {
        @Override
        public void onResponse(Response<GetCustomCategoriesModel[]> response) {
            Log.d("MainActivity", "Status Code = " + response.code());
            if (response.isSuccess()) {
                // request successful (status code 200, 201)
                GetCustomCategoriesModel[] result = response.body();
                Log.d("MainActivity", "response = " + new Gson().toJson(result));
                if (result.length > 0) {
                    dialog.dismiss();
                    mArray = result;
                    adapter = new CustomCategoriesAdapter(ProfessionalListActivity.this, mArray);
                    mLvCategories.setAdapter(adapter);
                    mLvCategories.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent mIntent = new Intent(ProfessionalListActivity.this, SubListProActivity.class);
                            mIntent.putExtra("sub_id", mArray[position].getCategory_id() + "");
                            startActivity(mIntent);
                        }
                    });
                }
            } else {
                // response received but request not successful (like 400,401,403 etc)
                //Handle errors
                Toast.makeText(ProfessionalListActivity.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
                Log.d("ProfessionalListActivity", "Response is not success");
            }
        }

        @Override
        public void onFailure(Throwable t) {
            dialog.dismiss();
            Log.d("ProfessionalListActivity", "On Failure");
        }
    });

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    if (LogInActivity.userLoginViaFb) {
        AlertDialog.Builder builder = new AlertDialog.Builder(ProfessionalListActivity.this);
        builder.setIcon(R.drawable.find_pro);
        builder.setTitle("Find Pro").setMessage("Are you sure you want to exit?")
                // Setting Icon to Dialog
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
        builder.show();
    }
    else{
        super.onBackPressed();
    }

}

请帮忙。

编辑:

public class RestClient {


    private static GitApiInterface gitApiInterface ;
    public static String baseUrl = "http://scorpioinfotech.net/demo" ;
    public static GitApiInterface getClient() {
        if (gitApiInterface == null) {

            OkHttpClient okClient = new OkHttpClient();
            okClient.interceptors().add(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Response response = chain.proceed(chain.request());
                    return response;
                }
            });

            Retrofit client = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverter(String.class, new ToStringConverter())
                    .client(okClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            gitApiInterface = client.create(GitApiInterface.class);
        }
        return gitApiInterface ;
    }

    public interface GitApiInterface {

        @GET
        Call<GetCustomCategoriesModel[]> hitGetApi(@Url String url);

        @GET
        Call<Lister[]> hitGetIntroApi (@Url String url);

        @GET
        Call<GetProDetailInfoModel> hitGetDetailinfoApi (@Url String url);

        @GET
        Call<Reviews[]> hitGetFeedbackApi (@Url String url);



    }
}

【问题讨论】:

  • 你在使用同步调用吗?
  • 大多数情况下,当您启动您的应用程序时,它会发生 bcoz 改造第一次调用需要时间,之后它会顺利......做一件事只需使用进度条,直到您的数据没有更新。
  • 我也在使用进度对话框。但它会停止,一段时间后屏幕变黑
  • 你的意思是让应用崩溃?
  • 没有应用没有崩溃,只是出现黑屏

标签: android parsing retrofit


【解决方案1】:

发生这种情况是因为您从 Web 将图像加载到适配器中而没有缩放它们,这是一项非常昂贵且资源密集型的操作。您应该尝试缩放它们,然后设置图像。空白屏幕是因为在 UI 线程上完成了太多工作。

【讨论】:

  • @PriyankaMinhas 如果它解决了您的问题,请您投票并接受答案!
  • 又是白屏问题
  • 哦,奇怪!很久了吗?
  • 是的,我需要按下设备的后退按钮,然后再次进入活动以获取详细信息
【解决方案2】:

终于不再出现黑屏了。我只是添加了这两行,因为超时异常即将到来: 公共类 RestClient {

 private static GitApiInterface gitApiInterface ;
    public static String baseUrl = "http://scorpioinfotech.net/demo" ;
    public static GitApiInterface getClient() {
        if (gitApiInterface == null) {
            OkHttpClient okClient = new OkHttpClient();

            okClient.setReadTimeout(100, TimeUnit.MINUTES); // added
            okClient.setConnectTimeout(300,TimeUnit.MINUTES); //added

            okClient.interceptors().add(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Response response = chain.proceed(chain.request());
                    return response;
                }
            });

            Retrofit client = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverter(String.class, new ToStringConverter())
                    .client(okClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            gitApiInterface = client.create(GitApiInterface.class);
        }
        return gitApiInterface ;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 2021-11-06
    • 1970-01-01
    • 2015-06-21
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    相关资源
    最近更新 更多