当前位置: 首页 > 知识库问答 >
问题:

我发布了一些值,并在改型中以数组形式得到响应,但出现了错误

万俟震博
2023-03-14

错误提示:

通用域名格式。谷歌。格森。JsonSyntaxException:java。lang.IllegalStateException:应为BEGIN\u对象,但在改装中的第1行第2列路径$处为BEGIN\u数组

这是我的职责:

public interface I_GetStockTransferForLocation {
    @FormUrlEncoded
    @POST("/DataAccessService.svc/GetStockTransferForLocation")
    void getStockFrLocation(@Field("COMPANY_ID")
    String companyId ,@Field("LOCATION_ID")
    String locationId ,@Field("TRANSACTION_DATE")
    String transactionDate ,
        Callback<RFResp_GetStockTransferFrLocatn> callback); } 

public class RFResp_GetStockTransferFrLocatn { 
    @SerializedName("StockTransferList") private List<StockTransferList> 
    stockTransferList = new ArrayList<StockTransferList>(); 
    @SerializedName("Transfer_Date")
    private String transferDate; }

private void getStockTransferFrLocation() {

    RestAdapter adapterGetStockTransfer = new RestAdapter.Builder()
            .setEndpoint(ROOT_URL) //Setting the Root URL
            .build(); //Finally building the adapter

    //Creating object for our interface
    I_GetStockTransferForLocation api = adapterGetStockTransfer.create(I_GetStockTransferForLocation.class);

    api.getStockFrLocation(strCompanyId, strLocationId, strST_date, new Callback<RFResp_GetStockTransferFrLocatn>() {
        @Override
        public void success(RFResp_GetStockTransferFrLocatn rfResp_getStockTransferFrLocatn, retrofit.client.Response response) {

            List<StockTransferList> stockTransferLists = rfResp_getStockTransferFrLocatn.getStockTransferList();

            db = new Database(context);
            stckTransferFk = db.getLastStock();

            if (stckTransferFk == 0){

                stckTransferFk = 1;
            }
            else{
                stckTransferFk =  stckTransferFk + 1;
            }

            if(stockTransferLists.size()>0) {

                db = new Database(context);
                db.clearStockTransferData();
                db.createStockTransfer(rfResp_getStockTransferFrLocatn.getTransferDate(), stockTransferLists,stckTransferFk);

                Intent intent = new Intent("com.dj.prachi");
                intent.putExtra("customers_count",db.getCustomerCount()+"");
                intent.putExtra("products_count",db.getProductCount()+"");
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

                Log.e("Prachi","Stock Transfer array"+stockTransferLists.size());
            }
        }

        @Override
        public void failure(RetrofitError error) {

            Log.e("Prachi","Stock Transfr Error"+error.getMessage());
        }
    });
}

我的回答是

[
  {
    "StockTransferList": [
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12107",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12108",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "30.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12109",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "30.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12110",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12111",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "30.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12112",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "30.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12113",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12114",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12115",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12116",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12117",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12118",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12119",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      },
      {
        "CLOSING_STOCK_QTY": "0.00",
        "COMPANY_ID": "7",
        "LOCATION_ID": "1",
        "OPENING_STOCK_QTY": "0.00",
        "STOCKITEM_ID": "12120",
        "TRANSACION_ID": "0",
        "TRANSFER_IN_QTY": "0.00"
      }
    ],
    "Transfer_Date": "25/06/2016"
  }
]

共有2个答案

韩夕
2023-03-14

如下创建父pojo并在该pojo中获取响应:

public class ParentPojo extends List<RFResp_GetStockTransferFrLocatn > {
}

您的方法如下所示:

private void getStockTransferFrLocation() {

RestAdapter adapterGetStockTransfer = new RestAdapter.Builder()
        .setEndpoint(ROOT_URL) //Setting the Root URL
        .build(); //Finally building the adapter

//Creating object for our interface
I_GetStockTransferForLocation api = adapterGetStockTransfer.create(I_GetStockTransferForLocation.class);

api.getStockFrLocation(strCompanyId, strLocationId, strST_date, new Callback<RFResp_GetStockTransferFrLocatn>() {
    @Override
    public void success(ParentPojo rfResp_getStockTransferFrLocatn, retrofit.client.Response response) {

        for(int i=0;i<rfResp_getStockTransferFrLocatn.length;i++){
        List<RFResp_GetStockTransferFrLocatn > stockTransferLists= rfResp_getStockTransferFrLocatn.get(i).getStockTransferList();

        db = new Database(context);
        stckTransferFk = db.getLastStock();

        if (stckTransferFk == 0){

            stckTransferFk = 1;
        }
        else{
            stckTransferFk =  stckTransferFk + 1;
        }

        if(stockTransferLists.size()>0) {

            db = new Database(context);
            db.clearStockTransferData();
            db.createStockTransfer(rfResp_getStockTransferFrLocatn.getTransferDate(), stockTransferLists,stckTransferFk);

            Intent intent = new Intent("com.dj.prachi");
            intent.putExtra("customers_count",db.getCustomerCount()+"");
            intent.putExtra("products_count",db.getProductCount()+"");
            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

            Log.e("Prachi","Stock Transfer array"+stockTransferLists.size());
        }
      }
    }

    @Override
    public void failure(RetrofitError error) {

        Log.e("Prachi","Stock Transfr Error"+error.getMessage());
    }
});
}
白吕恭
2023-03-14

问题似乎与您如何设置调用I\u GetStockTransferForLocation有关,从代码来看,它似乎期望得到一个对象RFResp\u GetStockTransferFrLocatn,但实际上从您发布的json来看,您应该得到这些对象的列表。

您只需更改I\u GetStockTransferForLocation的定义,即可使用列表

您还需要将回调从void success(RFResp\u GetStockTransferFrLocatn RFResp\u GetStockTransferFrLocatn,reformation.client.Response)调整为void success(列表

如果你看一下你得到的错误,应该是BEGIN\u OBJECT,但它是BEGIN\u ARRAY,这是gson的意思,意思是他需要一个带有对象的json,但实际上它得到的是一个对象列表。在您的情况下,这就是界面的工作方式:

public interface I_GetStockTransferForLocation { 
   @FormUrlEncoded          
   @POST("/DataAccessService.svc/GetStockTransferForLocation") 
   void getStockFrLocation(@Field("COMPANY_ID") String companyId,
                           @Field("LOCATION_ID") String locationId,
                           @Field("TRANSACTION_DATE") String transactionDate,
                           Callback<List<RFResp_GetStockTransferFrLocatn>> callback); }

如果你需要更多帮助,请告诉我。

 类似资料: