代码:
public void getMovie() { final ProgressDialog pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); pd.setMessage("加载中"); pd.show(); System.out.println("打开***"); String baseUrl = "https://api.douban.com/v2/movie/"; Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(ScalarsConverterFactory.create()) .build(); MovieService movieService = retrofit.create(MovieService.class); Call<String> call = movieService.getTopMovie(0, 10); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { System.out.println("获取数据成功***"); tvRx.setText(response.body().toString()); System.out.println("**********" + response.body()); // pd.dismiss(); Gson gson = new Gson(); } @Override public void onFailure(Call<String> call, Throwable throwable) { System.out.println("--------------------"); tvRx.setText(throwable.getMessage()); } }); Toast.makeText(getApplicationContext(), "ssdfa", Toast.LENGTH_SHORT).show(); System.out.println("关闭***"); pd.dismiss(); }
这段代码中,ProgressDialog始终是不显示,查看输出才明白用的异步请求导致ProgressDialog刚打开就被关闭了。
打印结果:
06-08 03:30:11.011 24465-24465/com.example.z.study I/System.out: 打开*** 06-08 03:30:11.042 24465-24465/com.example.z.study I/System.out: 关闭*** 06-08 03:30:12.514 24465-24465/com.example.z.study I/System.out: 获取数据成功***
在Node.js中也是同样的道理。在《深入浅出Node.js》中有这样一段话: