去到 build.gradle 文件那里 最下面的dependency 改一下
dependencies
{
implementation
fileTree(dir
: 'libs', include
: ['*.jar'])
implementation
'androidx.appcompat:appcompat:1.1.0'
implementation
'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation
'junit:junit:4.12'
androidTestImplementation
'androidx.test.ext:junit:1.1.1'
androidTestImplementation
'androidx.test.espresso:espresso-core:3.2.0'
implementation
'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation group
: 'com.squareup.okhttp3', name
: 'okhttp', version
: '4.9.0'
}
开启网络权限
<?xml version
="1.0" encoding
="utf-8"?>
<manifest xmlns
:android
="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses
-permission android
:name
="android.permission.INTERNET"></uses
-permission
>
@Override
public void onClick(View v
) {
switch (v
.getId()) {
case R
.id
.btn_clear
: {
Log
.i("click,trigger","设置焦点");
username
.setText("");
password
.setText("");
username
.requestFocus();
break;
}
case R
.id
.login
: {
WorkerUtil
.doWork(() -> {
OkHttpClient client
= new OkHttpClient();
Request request
= new Request.Builder()
.url("http://www.itshare.club/api/blog?blogId=16")
.get().build();
Call call
= client
.newCall(request
);
call
.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call
, @NotNull IOException e
) {
}
@Override
public void onResponse(@NotNull Call call
, @NotNull Response response
) throws IOException
{
System
.out
.println(("res"+ response
.body().string() ));
}
});
});
showToast("登录成功", Toast
.LENGTH_LONG
);
break;
}
}