当我只添加一个项目时,retrofit会产生500个内部服务器错误,但邮递员响应正常

时间:2017-03-28 10:21:33

标签: android retrofit2

我正在尝试调试此问题" Http 500内部访问错误"为此,代码如下所示:

private void submitOrder() {
    List<String> itemIds = new ArrayList<>();
    List<Integer> itemQuantity = new ArrayList<>();

    for (FoodTruckItem foodTruckItem : foodTruckItemList) {
        itemIds.add(foodTruckItem.getItemId());
        itemQuantity.add(foodTruckItem.getItemQuantityOrdered());
    }


    if (InternetConnection.checkConnection(this)) {
        Observable<GenericResponse> results = RetroClient.getApiService()
                .submitOrder("******", "", total,
                        "anywhere", "", "*****", itemIds, itemQuantity,
                        foodtruckInfo.getFoodTruckId());
        results.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<GenericResponse>() {
                    @Override
                    public void onCompleted() {
                        unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(MyCartActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onNext(GenericResponse response) {
                        showOrderCompleteFragment();
                    }

                });
    } else {

    }
}

在这里,当我发送带有2个项目的webservice时这会正常工作,但对于一个项目,它会产生500个内部服务器错误。但是对于每个案例邮递员成功创建订单

邮递员的回复如下所示:

web service response

1 个答案:

答案 0 :(得分:0)

我认为您需要使用Map itemIdsitemQuantity代替List<String>/List<Integer>,如下所示:

RetroClient.getApiService().submitOrder()

中定义以下参数
@FieldMap Map<String, String> itemIdsMap
@FieldMap Map<String, String> itemQuantityMap

现在传递itemIdsitemQuantity值,如下所示:

Map<String, String> itemIdsMap = new HashMap<>();
Map<String, String> itemQuantityMap = new HashMap<>();

String itemIds = "item_id[%d]";
String itemQuantity = "item_quantity[%d]";

for (int i = 0; i < foodTruckItemList.size(); i++) {
     itemIdsMap.put(String.format(itemIds, i), foodTruckItemList.get(i).getItemId());
     itemQuantityMap.put(String.format(itemQuantity, i), foodTruckItemList.get(i).getItemQuantityOrdered());                
}