万能适配器
导入依赖
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'
主布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/smart_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyler_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
OkHttpUtils工具类
public class OkHttpUtils {
private OkHttpClient okHttpClient;
private Handler handler = new Handler();
private OkHttpUtils() {
okHttpClient = new OkHttpClient.Builder()
.writeTimeout(60 * 1000, TimeUnit.MILLISECONDS)
.readTimeout(60 * 1000, TimeUnit.MILLISECONDS)
.build();
}
private static OkHttpUtils okHttpUtils = new OkHttpUtils();
public static OkHttpUtils getInstance() {
return okHttpUtils;
}
public void doget(String url, final OkhttpCallBack okhttpCallBack) {
Request request = new Request.Builder().get().url(url).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
final String message = e.getMessage();
handler.post(new Runnable() {
@Override
public void run() {
okhttpCallBack.onError(message);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
okhttpCallBack.onOk(string);
}
});
}
});
}
public void dopost(String url, HashMap<String, String> map, final OkhttpCallBack okhttpCallBack) {
Set<Map.Entry<String, String>> entries = map.entrySet();
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry<String, String> entry : entries) {
builder.add(entry.getKey(), entry.getValue());
}
FormBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
okHttpClient.newCall