我有一个简单的网络调用,使用 Jackson 进行转换器,设置如下
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("*****")
.addConverterFactory(JacksonConverterFactory.create(new
ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL)))
.build();
apiService = retrofit.create(APIService.class);
和
使用 获取 BGControl 对象列表
public interface APIService {
@GET("endpoint/{ID}")
Call<List<BGControl>> getBGControlData(@Path("ID") int patientId, @Query("start") String startDate, @Query("end") String endDate);
}
下面是 BGControl 类,
@JsonIgnoreProperties(ignoreUnknown = true)
public class BGControl {
String Activity;
String Glucose_mg_dL;
String Glucose_mmol_L;
String Timestamp;
String Classification;
String ReadingType;
int intervals = 0;
public BGControl() {
}
public BGControl(String activity, String glucose_mg_dL, String glucose_mmol_L, String timestamp, String classification, String readingType) {
Activity = activity;
Glucose_mg_dL = glucose_mg_dL;
Glucose_mmol_L = glucose_mmol_L;
Timestamp = timestamp;
Classification = classification;
ReadingType = readingType;
}
public void setActivity(String activity) {
Activity = activity;
}
public void setGlucose_mg_dL(String glucose_mg_dL) {
Glucose_mg_dL = glucose_mg_dL;
}
public void setGlucose_mmol_L(String glucose_mmol_L) {
Glucose_mmol_L = glucose_mmol_L;
}
public void setTimestamp(String timestamp) {
Timestamp = timestamp;
}
public void setClassification(String classification) {
Classification = classification;
}
public void setReadingType(String readingType) {
ReadingType = readingType;
}
public String getActivity() {
return Activity;
}
public String getGlucose_mg_dL() {
return Glucose_mg_dL;
}
public String getGlucose_mmol_L() {
return Glucose_mmol_L;
}
public String getTimestamp() {
return Timestamp;
}
public String getClassification() {
return Classification;
}
public String getReadingType() {
return ReadingType;
}
public int getIntervals() {
return intervals;
}
public void setIntervals(int intervals) {
this.intervals = intervals;
}
@Override
public String toString() {
return "BGControl{" +
"Activity='" + Activity + '\'' +
", Glucose_mg_dL='" + Glucose_mg_dL + '\'' +
", Glucose_mmol_L='" + Glucose_mmol_L + '\'' +
", Timestamp='" + Timestamp + '\'' +
", Classification='" + Classification + '\'' +
", ReadingType='" + ReadingType + '\'' +
'}';
}
}
下面是网络调用消费者代码
final Call<List<BGControl>> graphDataCall = NetworkManager.getInstance().getAPIService().getBGControlData(13614, startDate, endDate);
graphDataCall.enqueue(new Callback<List<BGControl>>() {
@Override
public void onResponse(Call<List<BGControl>> call, Response<List<BGControl>> response) {
List<BGControl> bgControlList = response.body();
if (bgControlList.size() > 0) {
}
}
@Override
public void onFailure(Call<List<BGControl>> call, Throwable t)
{
graphDataCall.cancel();
}
});
但是,问题是当我通过 Android studio 运行它时,它会在设备中安装 APK,我检查它,我得到了正确映射的响应,从而更新了 UI(我已经使用此响应渲染了多个图表!)( studio 运行 assembleProdDebug 命令)。但是当我使用设备“adb install -r ***.apk”中的命令行安装相同的 apk 时,响应将为空。
这很奇怪,我在手动安装和验证这个 apk 之前将此 apk 发送给客户端:(,现在这是一个问题。
如果您遇到类似问题,请提出解决方案。
最佳答案
您可能使用了 proguard 来缩小更改变量名称的代码。
您可以尝试在生成 apk 时将 build.gradle
文件中的 minifyEnabled
设置为 false。
如果您想将 proguard 与 Retrofit 一起使用,请检查此问题的答案:
关于java - 与通过 android studio 安装相比,作为 APK 安装时,Retrofit 的行为不同且无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46953965/