已经编写好的专门用于toast显示的函数
//提示信息显示
public void showToast(String msg){
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
}
通过在另外一个activity调用该方法showToast()
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
showToast(result);
}
出现了Can’t toast on a thread that has not called Looper.prepare(),这是因为主线程才能处理UI操作,在工作线程是不能处理UI操作的
此时我们可以通过以下方式解决该问题
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
showToast(result);
}
});
}
让我们的Toast方法在主线程执行