使用 POST Body with Form-Data in Android 调用时改造不返回 JSON 对象
Retrofit not returning JSON object when called using POST Body with Form-Data in Android
我目前正在开发一个 Android 应用程序,其中我正在使用 Retrofit 访问具有 POST 表单数据的 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
public class 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);
}
来自 Postman 的快照
试试这个方法
@FormUrlEncoded
@POST("/Desiredpath")
Call<List<SapRevenue>> getData(@Field("stdate") String stdate);
从 here
创建你的 pojo class
希望这项工作:)
我认为问题出在这里,您想将 json 解析为列表,但实际上服务器 return 为您提供了一个包含列表的对象。
所以你的回复 return 类型应该是这样的:
Call<SapRevenueResponse>
其中 SapRevenueResponse:
SapRevenueResponse {
... List ...
}
我目前正在开发一个 Android 应用程序,其中我正在使用 Retrofit 访问具有 POST 表单数据的 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
public class 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);
}
来自 Postman 的快照
试试这个方法
@FormUrlEncoded
@POST("/Desiredpath")
Call<List<SapRevenue>> getData(@Field("stdate") String stdate);
从 here
创建你的 pojo class希望这项工作:)
我认为问题出在这里,您想将 json 解析为列表,但实际上服务器 return 为您提供了一个包含列表的对象。
所以你的回复 return 类型应该是这样的:
Call<SapRevenueResponse>
其中 SapRevenueResponse:
SapRevenueResponse {
... List ...
}