Using Retrofit and Gson and getting error: Skipped 100 frames! The application may be doing too much work on its main thread

Using Retrofit and Gson and getting error: Skipped 100 frames! The application may be doing too much work on its main thread

我正在使用 Retrofit 进行 API 调用,但我一直收到错误消息:跳过 100 帧!应用程序可能在其主线程上做了太多工作。

我该如何解决?我认为所有的线程问题都通过改造本身来解决。现在,我想我知道为什么会这样了,UI 线程有很多事情要做,所以它会跳过一些东西。但是如何解决这个问题。在下面的代码中你可以看到我每次都需要为 url 发送不同的条件,那么如何创建一个单独的线程或异步任务来完成这个?

谢谢。

public class Home extends Activity {

    private GridView gv_search_categories_lay;
    private TextView tv_title_header;
    private ImageView im_icon_header, im_search_header, im_cart_header;
    private Button b_search;
    private EditText ed_search;

    private String
            base_url = null,
            file = null,
            operation = null,
            all = null,
            max_depth = null;

    private String[] category_id = {}, category_name = {}, parent_id = {}, node_depth = {};

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_act);
        init();

        gv_search_categories_lay.setAdapter(new HomeCategoryL1GridAdapter(this, category_id,
                category_name, parent_id, node_depth,
                icons));
    }

    void init() {
        findViews();
        changeFont();
        assignConditions("category", "all", "0");
        categoryAllApiCall();
    }

    void findViews() {
        im_icon_header = (ImageView) findViewById(R.id.im_icon_header);
        im_search_header = (ImageView) findViewById(R.id.im_search_header);
        im_cart_header = (ImageView) findViewById(R.id.im_cart_header);
        tv_title_header = (TextView) findViewById(R.id.tv_title_header);
        gv_search_categories_lay = (GridView) findViewById(R.id.gv_search_categories_lay);
        b_search = (Button) findViewById(R.id.b_search_category_lay_search);
        ed_search = (EditText) findViewById(R.id.ed_search_category_lay_search);
    }

    void changeFont() {
        tv_title_header.setTypeface(EasyFonts.robotoMedium(this));
        b_search.setTypeface(EasyFonts.robotoLight(this));
        ed_search.setTypeface(EasyFonts.robotoLight(this));
    }

    void assignConditions(String operation,
                          String all,
                          String max_depth) {
        this.base_url = getResources().getString(R.string.base_url);
        this.file = getResources().getString(R.string.file);
        this.operation = operation;
        this.all = all;
        this.max_depth = max_depth;
    }

    void categoryAllApiCall() {


        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(base_url).build();
        final Category_All category_all = restAdapter.create(Category_All.class);
        category_all.getFeed(file, operation, all, max_depth, new Callback<CategoryPojo>() {

                    @Override
                    public void success(CategoryPojo categoryPojo, Response response) {
                        Home.this.category_id = Arrays.copyOf(categoryPojo.getCategoryId(),
                                categoryPojo.getCategoryId().length);
                        Home.this.category_name = Arrays.copyOf(categoryPojo.getCategoryName(),
                                categoryPojo.getCategoryName().length);
                        Home.this.parent_id = Arrays.copyOf(categoryPojo.getParentId(),
                                categoryPojo.getParentId().length );
                        Home.this.node_depth = Arrays.copyOf(categoryPojo.getNodeDepth(),
                                categoryPojo.getNodeDepth().length);
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        tv_title_header.setText(error.getMessage());
                    }
                });
    }

}

在UI线程中执行的回调成功或失败方法中的代码。您在那里进行了大量的数组复制操作。您可以尝试将它们放在单独的线程中。

编辑:如何在单独的线程中运行的代码

void categoryAllApiCall() {


    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(base_url).build();
    final Category_All category_all = restAdapter.create(Category_All.class);
    category_all.getFeed(file, operation, all, max_depth, new Callback<CategoryPojo>() {

                @Override
                public void success(CategoryPojo categoryPojo, Response response) {
                    new Thread(new Runnable() {
                         @Override
                         public void run() {
                             Home.this.category_id = Arrays.copyOf(categoryPojo.getCategoryId(),
                            categoryPojo.getCategoryId().length);
                             Home.this.category_name = Arrays.copyOf(categoryPojo.getCategoryName(),
                            categoryPojo.getCategoryName().length);
                             Home.this.parent_id = Arrays.copyOf(categoryPojo.getParentId(),
                            categoryPojo.getParentId().length );
                             Home.this.node_depth = Arrays.copyOf(categoryPojo.getNodeDepth(),
                            categoryPojo.getNodeDepth().length);                    
                         }
                    }).run();
                }

                @Override
                public void failure(RetrofitError error) {
                    tv_title_header.setText(error.getMessage());
                }
            });
}