MVC

layout.activity_main);

initViews();

request();
showDialog();
}

private void initViews() {
tv = findViewById(R.id.tv);
}

private void showDialog() {
ProgressDialog dialog = new ProgressDialog(this);
dialog.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
}, 1500);
}

private void request() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://www.baidu.com/")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}

@Override
public void onResponse(Call call, Response response) throws IOException {
String resp = response.body().string();
toast(resp);
}
});
}

private void toast(String resp) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "" + resp, Toast.LENGTH_SHORT).show();
tv.setText(resp);
}
});
}

}
---------------------

原文地址:https://www.cnblogs.com/ly570/p/11342618.html