问题描述
我目前正在开发一个Android应用,其中我正在使用Retrofit通过POST Form-Data访问API。该API在POSTMAN中可以正常运行,但在Android中却报错
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
请建议我在哪里做错了。预先感谢。
我的改装代码
long date = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd",Locale.US);
String dateString = sdf.format(date);
Gson gson = new GsonBuilder()
.setLenient()
.create();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
dataArrayList = new ArrayList<>();
SapRevenueService revenueApiService = retrofit.create(SapRevenueService.class);
Call<List<SapRevenue>> call = revenueApiService.getData(String.valueOf(dateString));
call.enqueue(new Callback<List<SapRevenue>>() {
@Override
public void onResponse(@NotNull Call<List<SapRevenue>> call,@NotNull Response<List<SapRevenue>> response) {
dataArrayList = response.body();
Log.d(TAG,"Number of records received: " + dataArrayList.size());
}
@Override
public void onFailure(@NotNull Call<List<SapRevenue>> call,@NotNull Throwable throwable) {
Log.e(TAG,throwable.toString());
}
});
我的POJO
公共类SapRevenue实现了可序列化,可比较的{
public String locationCode,arrearCollection,currentCollection,arrearDemand,currentDemand,unitsBilled;
public SapRevenue(String locationCode,String asOndate,String arrearCollection,String currentCollection,String arrearDemand,String currentDemand,String unitsBilled) {
this.locationCode = locationCode;
this.arrearCollection = arrearCollection;
this.currentCollection = currentCollection;
this.arrearDemand = arrearDemand;
this.currentDemand = currentDemand;
this.unitsBilled = unitsBilled;
}
public String getLocationCode() {
return locationCode;
}
public void setLocationCode(String locationCode) {
this.locationCode = locationCode;
}
public String getArrearCollection() {
return arrearCollection;
}
public void setArrearCollection(String arrearCollection) {
this.arrearCollection = arrearCollection;
}
public String getCurrentCollection() {
return currentCollection;
}
public void setCurrentCollection(String currentCollection) {
this.currentCollection = currentCollection;
}
public String getArrearDemand() {
return arrearDemand;
}
public void setArrearDemand(String arrearDemand) {
this.arrearDemand = arrearDemand;
}
public String getCurrentDemand() {
return currentDemand;
}
public void setCurrentDemand(String currentDemand) {
this.currentDemand = currentDemand;
}
public String getUnitsBilled() {
return unitsBilled;
}
public void setUnitsBilled(String unitsBilled) {
this.unitsBilled = unitsBilled;
}
}
界面
public interface SapRevenueService {
@POST("/Desiredpath")
Call<List<SapRevenue>> getData(@Body String stdate);
}
邮差的快照
解决方法
尝试这种方式
@FormUrlEncoded
@POST("/Desiredpath")
Call<List<SapRevenue>> getData(@Field("stdate") String stdate);
从here
创建您的pojo类希望这项工作:)
,我相信这里是您想将json解析为List的问题,但实际上服务器会向您返回一个内部包含list的对象。
因此您的响应返回类型应类似于:
Call<SapRevenueResponse>
其中SapRevenueResponse:
SapRevenueResponse {
... List ...
}