This commit is contained in:
liux 2025-03-18 10:13:29 +08:00
parent 3887a364ce
commit a0bf82c736
1 changed files with 50 additions and 2 deletions

View File

@ -36,8 +36,7 @@ public class OkHttpService {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private String token =
"eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjozNzkyNTg1Mjk2NDQ1NDgwOTYsInVzZXJfa2V5IjoiYmM2MWY0NDAtNmFmOS00OWI4LWI1N2QtZjUyYmMzZTIxM2Y1IiwidXNlcm5hbWUiOiJkZTU4ZjYzNjU3Y2RhOGVkYjZiZTg3NTExMGUyMDNmNCJ9.sQ7xmKxfgTL7seFzBmHzTezL050bMTtrxagK1gfwDklSZf4iMm6uHZBoqrxbwMbbtcnJn7JQEOj3hBGCFZj5Rg";
private String token = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjozNzk0Nzk0NzUyMTkzMzcyMTYsInVzZXJfa2V5IjoiYmQ1MDJjYjktNDZiZi00MjQ0LTgwYjctOGZhNTJmNzc0N2UzIiwidXNlcm5hbWUiOiJlNjM1NjJkZWExZDhlMTRjZTMwN2M2MTNiYzNkNWNiOSJ9.wVwvOKDQtI72wL0ypGFjuFjLbiScn8k7ZXXRExSSuifC_77WToR6jrpHhsvL6hGMm3Hm-DgnPuJcV3YGTmaefQ";
private volatile static OkHttpClient client;
@ -205,6 +204,55 @@ public class OkHttpService {
return "";
}
public String httpPost(String url, RequestBody body, Context context) {
try {
OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(60000, TimeUnit.MILLISECONDS)
.readTimeout(60000, TimeUnit.MILLISECONDS)
.build();
Request request = new Request.Builder().url(url)
.addHeader("Authorization", token)
.addHeader("machine-sn","2AD2428001118585")
.post(body).build();
Response response = client.newCall(request).execute();
int statueCode = response.code();
Log.e(TAG, statueCode + "");
if (statueCode == 200) {
String json = response.body().string();
Log.e(TAG, "json=" + response.toString());
if (!StringHelper.isEmpty(json) && json.contains("OK")) {
return json;
}
return json;
}else if (statueCode==401){
ThreadPoolManager.executeInUIThread(new Runnable() {
@Override
public void run() {
// 在这里执行UI操作如创建并显示AlertDialog
// 使用JSON解析库解析主体内容
JSONObject jsonResponse = null;
try {
String responseBody = response.body().string();
jsonResponse = new JSONObject(responseBody);
String message = jsonResponse.getString("message");
if (message.contains("过期")){
XToastUtils.warning("登录过期,请重新登录");
}
} catch (JSONException | IOException e) {
e.printStackTrace();
}
}
});
} else {
return "";
}
} catch (Exception e) {
Log.i(TAG, e.toString());
}
return "";
}
public String httpPostNoToken(String url, FormBody body) {
try {
OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(60000, TimeUnit.MILLISECONDS)