上传文件至 /
This commit is contained in:
parent
256a553c96
commit
4802e112fb
|
@ -0,0 +1,38 @@
|
||||||
|
package com.example.myapplication.Tool;
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class RetrofitClient {
|
||||||
|
private static final String BASE_URL = "http://pms.dtyx.net:9158/";
|
||||||
|
private static Retrofit retrofit;
|
||||||
|
|
||||||
|
public static Retrofit getClient(String authToken) {
|
||||||
|
if (retrofit == null) {
|
||||||
|
// 创建 OkHttpClient 并添加拦截器
|
||||||
|
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
|
||||||
|
.connectTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(30, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
// 添加日志拦截器(可选)
|
||||||
|
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
|
||||||
|
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
|
||||||
|
okHttpClientBuilder.addInterceptor(loggingInterceptor);
|
||||||
|
|
||||||
|
// 添加认证拦截器(自动添加 Authorization header)
|
||||||
|
okHttpClientBuilder.addInterceptor(new AuthInterceptor(authToken));
|
||||||
|
|
||||||
|
// 构建 Retrofit
|
||||||
|
retrofit = new Retrofit.Builder()
|
||||||
|
.baseUrl(BASE_URL)
|
||||||
|
.client(okHttpClientBuilder.build())
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
return retrofit;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.example.myapplication.Tool;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.ClipboardManager;
|
||||||
|
import android.content.ClipData;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AlertDialog;
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
|
public class ShowError {
|
||||||
|
private Context context;
|
||||||
|
private final Handler mainHandler;
|
||||||
|
public ShowError(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
this.mainHandler = new Handler(Looper.getMainLooper());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showErrorDialog(String title, String message) {
|
||||||
|
if (context instanceof AppCompatActivity) {
|
||||||
|
AppCompatActivity activity = (AppCompatActivity) context;
|
||||||
|
activity.runOnUiThread(() -> {
|
||||||
|
new AlertDialog.Builder(activity)
|
||||||
|
.setTitle(title)
|
||||||
|
.setMessage(message)
|
||||||
|
.setPositiveButton("确定", null)
|
||||||
|
.setNegativeButton("复制错误", (dialog, which) -> {
|
||||||
|
// 将错误复制到剪贴板
|
||||||
|
ClipboardManager clipboard =
|
||||||
|
(ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
|
ClipData clip =
|
||||||
|
ClipData.newPlainText("错误信息", message);
|
||||||
|
clipboard.setPrimaryClip(clip);
|
||||||
|
showToast("已复制错误信息");
|
||||||
|
})
|
||||||
|
.show();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showToast(String message) {
|
||||||
|
mainHandler.post(() -> Toast.makeText(context, message, Toast.LENGTH_SHORT).show());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue