gpt4 book ai didi

android - Retrofit 2 - 如何在接收 JSON 响应时显示进度条

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:44 32 4
gpt4 key购买 nike

我在选项卡式 Activity 中使用多个 fragment 来显示 json 数据。
我想在每个 fragment 收到响应时显示进度条。

private void loadJSON() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
newsAPI = retrofit.create(NewsAPI.class);
Call<JSONResponse> call = newsAPI.topNews("soure", "api-key");

call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
Log.v("this", "Yes!");
}

@Override public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.v("this", "No Response!");
}
});
}

最佳答案

像这样,使用 progressDialog :

private void loadJSON() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
newsAPI = retrofit.create(NewsAPI.class);
Call < JSONResponse > call =
newsAPI.topNews("soure", "api-key");

// Set up progress before call
final ProgressDialog progressDoalog;
progressDoalog = new ProgressDialog(MainActivity.this);
progressDoalog.setMax(100);
progressDoalog.setMessage("Its loading....");
progressDoalog.setTitle("ProgressDialog bar example");
progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// show it
progressDoalog.show();

call.enqueue(new Callback < JSONResponse > () {

@Override
public void onResponse(Call < JSONResponse > call, Response < JSONResponse > response) {
// close it after response
progressDoalog.dismiss();
Log.v("this", "Yes!");
}
}

@Override public void onFailure(Call < JSONResponse > call, Throwable t) {
// close it after response
progressDoalog.dismiss();
Log.v("this", "No Response!");
}
});

关于android - Retrofit 2 - 如何在接收 JSON 响应时显示进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42301818/

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