|
|
|
@ -16,6 +16,7 @@ import android.widget.TextView; |
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
|
import androidx.appcompat.app.AlertDialog; |
|
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout; |
|
|
|
|
import androidx.lifecycle.MutableLiveData; |
|
|
|
|
|
|
|
|
|
import com.community.pocket.BuildConfig; |
|
|
|
|
import com.community.pocket.R; |
|
|
|
@ -65,47 +66,41 @@ public class HttpUtil { |
|
|
|
|
call.enqueue(callback); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//关闭弹窗
|
|
|
|
|
private static AlertDialog.Builder getBuilder(Context context) { |
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context); |
|
|
|
|
View view = View.inflate(context, R.layout.debug_title, null); |
|
|
|
|
ImageView imageView = view.findViewById(R.id.close); |
|
|
|
|
imageView.setOnClickListener(v -> { |
|
|
|
|
View confirm = View.inflate(context, R.layout.debug_alert, null); |
|
|
|
|
AlertDialog.Builder dialog = new AlertDialog.Builder(context); |
|
|
|
|
dialog.setView(confirm); |
|
|
|
|
final AlertDialog alertDialog = dialog.create(); |
|
|
|
|
confirm.findViewById(R.id.yes).setOnClickListener(v1 -> System.exit(0)); |
|
|
|
|
confirm.findViewById(R.id.no).setOnClickListener(v12 -> alertDialog.dismiss()); |
|
|
|
|
alertDialog.show(); |
|
|
|
|
}); |
|
|
|
|
builder.setCustomTitle(view); |
|
|
|
|
return builder; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//检查服务端连接
|
|
|
|
|
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(); |
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context); |
|
|
|
|
View view = View.inflate(context, R.layout.debug_title, null); |
|
|
|
|
ImageView imageView = view.findViewById(R.id.close); |
|
|
|
|
imageView.setOnClickListener(new View.OnClickListener() { |
|
|
|
|
@Override |
|
|
|
|
public void onClick(View v) { |
|
|
|
|
View confirm = View.inflate(context, R.layout.debug_alert, null); |
|
|
|
|
AlertDialog.Builder dialog = new AlertDialog.Builder(context); |
|
|
|
|
dialog.setView(confirm); |
|
|
|
|
final AlertDialog alertDialog = dialog.create(); |
|
|
|
|
confirm.findViewById(R.id.yes).setOnClickListener(new View.OnClickListener() { |
|
|
|
|
@Override |
|
|
|
|
public void onClick(View v) { |
|
|
|
|
System.exit(0); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
confirm.findViewById(R.id.no).setOnClickListener(new View.OnClickListener() { |
|
|
|
|
@Override |
|
|
|
|
public void onClick(View v) { |
|
|
|
|
alertDialog.dismiss(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
alertDialog.show(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
builder.setCustomTitle(view); |
|
|
|
|
if (Patterns.WEB_URL.matcher(BuildConfig.API_HOST).matches()) { |
|
|
|
|
public static void checkServer(final Context context, MutableLiveData<Boolean> bool) { |
|
|
|
|
if (Patterns.WEB_URL.matcher(BuildConfig.API_HOST).matches()) { |
|
|
|
|
//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) { |
|
|
|
|
bool.postValue(false); |
|
|
|
|
if (BuildConfig.DEBUG) { |
|
|
|
|
Looper.prepare(); |
|
|
|
|
AlertDialog.Builder builder = getBuilder(context); |
|
|
|
|
ConstraintLayout constraintLayout = (ConstraintLayout) View.inflate(context, R.layout.debug, null); |
|
|
|
|
TextView buildType = constraintLayout.findViewById(R.id.build_type); |
|
|
|
|
buildType.setText(context.getString(R.string.build_type, BuildConfig.BUILD_TYPE)); |
|
|
|
@ -138,35 +133,41 @@ public class HttpUtil { |
|
|
|
|
testPort.setGravity(error.getGravity()); |
|
|
|
|
linearLayout.addView(testPort, index + 1); |
|
|
|
|
|
|
|
|
|
openUrl.setOnClickListener(new View.OnClickListener() { |
|
|
|
|
@Override |
|
|
|
|
public void onClick(View v) { |
|
|
|
|
//从其他浏览器打开
|
|
|
|
|
Intent intent = new Intent(); |
|
|
|
|
intent.setAction(Intent.ACTION_VIEW); |
|
|
|
|
Uri content_url = Uri.parse(BuildConfig.API_HOST); |
|
|
|
|
intent.setData(content_url); |
|
|
|
|
Activity activity = (Activity) context; |
|
|
|
|
activity.startActivity(Intent.createChooser(intent, context.getString(R.string.choose_browser))); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
openUrl.setOnClickListener(v -> { |
|
|
|
|
//从其他浏览器打开
|
|
|
|
|
Intent intent = new Intent(); |
|
|
|
|
intent.setAction(Intent.ACTION_VIEW); |
|
|
|
|
Uri content_url = Uri.parse(BuildConfig.API_HOST); |
|
|
|
|
intent.setData(content_url); |
|
|
|
|
Activity activity = (Activity) context; |
|
|
|
|
activity.startActivity(Intent.createChooser(intent, context.getString(R.string.choose_browser))); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
builder.setView(constraintLayout); |
|
|
|
|
} else { |
|
|
|
|
builder.setMessage(context.getString(R.string.error_api_host, BuildConfig.API_HOST)); |
|
|
|
|
builder.show(); |
|
|
|
|
Looper.loop(); |
|
|
|
|
} |
|
|
|
|
builder.show(); |
|
|
|
|
Looper.loop(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onResponse(@NotNull Call call, @NotNull Response response) { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onResponse(@NotNull Call call, @NotNull Response response) { |
|
|
|
|
bool.postValue(true); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
String msg = "\n"; |
|
|
|
|
if (BuildConfig.API_HOST.contains(" ")) { |
|
|
|
|
msg += context.getString(R.string.server_address_error_1); |
|
|
|
|
} else if (BuildConfig.API_HOST.contains(":") || BuildConfig.API_HOST.contains("。")) { |
|
|
|
|
msg += context.getString(R.string.server_address_error_2); |
|
|
|
|
} else if (!BuildConfig.API_HOST.startsWith("http")) { |
|
|
|
|
msg += context.getString(R.string.server_address_error_3); |
|
|
|
|
} else { |
|
|
|
|
msg += context.getString(R.string.server_address_error_4); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
getBuilder(context).setMessage(context.getString(R.string.server_address_error, BuildConfig.API_HOST) + msg).show(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public enum Method { |
|
|
|
@ -222,9 +223,7 @@ public class HttpUtil { |
|
|
|
|
if (httpRequest != null) { |
|
|
|
|
return httpRequest.value(); |
|
|
|
|
} |
|
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} catch (RuntimeException e) { |
|
|
|
|
} catch (ClassNotFoundException | RuntimeException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|