旧版Android升级遇到 Didn't find class "org.apache.commons.logging.impl.LogFactoryImpl" 和 Gson().fromJson().getType() Missing type parameter.

1.java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.logging.impl.LogFactoryImpl"

更换 'org.apache.httpcomponents:httpclient:4.4.1'为 implementation 'com.squareup.okhttp3:okhttp:4.9.3'
写法更换

 1 import okhttp3.*;
 2 import java.io.IOException;
 3 import java.util.concurrent.TimeUnit;
 4 
 5 private static final OkHttpClient client = new OkHttpClient.Builder()
 6             .connectTimeout(10, TimeUnit.SECONDS)
 7             .readTimeout(30, TimeUnit.SECONDS)
 8             .writeTimeout(30, TimeUnit.SECONDS)
 9             .build();
10 
11     public static String executeGetRequest(String url) throws IOException {
12         Request request = new Request.Builder()
13                 .url(url)
14                 .build();
15 
16         try (Response response = client.newCall(request).execute()) {
17             if (!response.isSuccessful()) {
18                 throw new IOException("Unexpected code " + response);
19             }
20             return response.body().string();
21         }
22     }
23 
24     public static String executePostRequest(String url, TreeMap<String, String> params) throws IOException {
25         FormBody.Builder formBuilder = new FormBody.Builder();
26         for (Map.Entry<String, String> entry : params.entrySet()) {
27             formBuilder.add(entry.getKey(), entry.getValue());
28         }
29         RequestBody formBody = formBuilder.build();
30 
31         Request request = new Request.Builder()
32                 .url(url)
33                 .post(formBody)
34                 .build();
35 
36         try (Response response = client.newCall(request).execute()) {
37             if (!response.isSuccessful()) {
38                 throw new IOException("Unexpected code " + response);
39             }
40             return response.body().string();
41         }
42     }

2.Gson().fromJson().getType()   Missing type parameter.

引入最新版

implementation 'com.google.code.gson:gson:2.10.1'

插入proguard-rules.pro

1 -keep class org.json.** {*;}
2 -keepattributes Signature
3 -keep class com.google.gson.reflect.TypeToken { *; }
4 -keep class * extends com.google.gson.reflect.TypeToken
5 -keepattributes AnnotationDefault,RuntimeVisibleAnnotations

使用案例

    public static LoginInfo login(LoginGroupBean bean) throws IOException {
        TreeMap<String, String> paramMap = new TreeMap<>();
        paramMap.put("UserId", bean.getGroupNo());
        paramMap.put("Password", bean.getPassword());
        String response = executePostRequest(Common.SERVER_URL + "login", paramMap);
        TypeToken<LoginInfo> typeToken = new TypeToken<LoginInfo>() {};
        return new Gson().fromJson(response, typeToken.getType());
}

 

posted on 2024-07-11 13:50  ShawYoi  阅读(9)  评论(0编辑  收藏  举报