gpt4 book ai didi

java - 使用 Retrofit 和 Gson 并收到错误 : Skipped 100 frames! 应用程序可能在其主线程上做了太多工作

转载 作者:行者123 更新时间:2023-12-02 04:30:20 24 4
gpt4 key购买 nike

我正在使用 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());
}
});
}

}

最佳答案

Callback 的 success 或 failure 方法中的代码在 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());
}
});
}

关于java - 使用 Retrofit 和 Gson 并收到错误 : Skipped 100 frames! 应用程序可能在其主线程上做了太多工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31560574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com