我正在发布一些值并在改进时将响应作为数组获取但是收到错误

时间:2016-06-27 05:51:17

标签: android mysql json retrofit

错误说:

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2行路径$中为BEGIN_ARRAY

这是我的功能:

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 个答案:

答案 0 :(得分:0)

如下所示创建父pojo并在该pojo中获得响应:

    $.ajax({
        url: "<?php echo site_url('connection/login')?>",
        type: "POST",
        data: Login_data,
        success: function () {
            alert('ajax worked');
        }
    });

您的方法如下所示:

public class ParentPojo extends List<RFResp_GetStockTransferFrLocatn > {
}

答案 1 :(得分:0)

这个问题似乎与您设置通话I_GetStockTransferForLocation的方式有关,这个问题从代码中看起来似乎只有一个对象 - RFResp_GetStockTransferFrLocatn,但实际上来自json,你是&#39} ;发布它看起来你应该得到这些对象的列表。

您只需要将I_GetStockTransferForLocation的定义更改为使用List<RFResp_GetStockTransferFrLocatn>而不仅仅是RFResp_GetStockTransferFrLocatn,您应该没问题。

您还需要将回调从void success(RFResp_GetStockTransferFrLocatn rfResp_getStockTransferFrLocatn, retrofit.client.Response response)调整为void success(List<RFResp_GetStockTransferFrLocatn> rfResp_getStockTransferFrLocatn, retrofit.client.Response response)

如果你看一下你得到的错误 - 预期的BEGIN_OBJECT但是BEGIN_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); }

如果您需要更多帮助,请与我联系。