|
|
|
@ -1,9 +1,17 @@ |
|
|
|
|
package com.community.pocket.util; |
|
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
|
import android.os.Looper; |
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
|
import androidx.appcompat.app.AlertDialog; |
|
|
|
|
|
|
|
|
|
import com.community.pocket.BuildConfig; |
|
|
|
|
import com.community.pocket.R; |
|
|
|
|
|
|
|
|
|
import org.jetbrains.annotations.NotNull; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
@ -14,6 +22,7 @@ import okhttp3.Callback; |
|
|
|
|
import okhttp3.FormBody; |
|
|
|
|
import okhttp3.OkHttpClient; |
|
|
|
|
import okhttp3.Request; |
|
|
|
|
import okhttp3.Response; |
|
|
|
|
|
|
|
|
|
public class HttpUtil { |
|
|
|
|
|
|
|
|
@ -43,6 +52,33 @@ public class HttpUtil { |
|
|
|
|
call.enqueue(callback); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//检查服务端连接
|
|
|
|
|
public static void checkServer(final Context context) { |
|
|
|
|
//1.创建OkHttpClient对象
|
|
|
|
|
OkHttpClient okHttpClient = new OkHttpClient(); |
|
|
|
|
//2.创建Request对象,设置一个url地址(百度地址),设置请求方式。
|
|
|
|
|
Request.Builder builder = new Request.Builder().url(BuildConfig.API_HOST); |
|
|
|
|
//3.创建一个call对象,参数就是Request请求对象
|
|
|
|
|
Call call = okHttpClient.newCall(builder.build()); |
|
|
|
|
//4.请求加入调度,重写回调方法
|
|
|
|
|
call.enqueue(new Callback() { |
|
|
|
|
@Override |
|
|
|
|
public void onFailure(@NotNull Call call, @NotNull IOException e) { |
|
|
|
|
if (BuildConfig.DEBUG) { |
|
|
|
|
Looper.prepare(); |
|
|
|
|
new AlertDialog.Builder(context) |
|
|
|
|
.setMessage(context.getString(R.string.check_server_fail, BuildConfig.API_HOST)).show(); |
|
|
|
|
Looper.loop(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onResponse(@NotNull Call call, @NotNull Response response) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public enum Method { |
|
|
|
|
GET, |
|
|
|
|
POST |
|
|
|
|