簡體   English   中英

嵌套Json使用改造

[英]Nested Json using retrofit

我想使用翻新從json中讀取sub_category> id,name

但是下面的代碼給出了失敗的響應。

java.lang.IllegalStateException:預期為BEGIN_OBJECT,但在第2行第2列路徑$處為BEGIN_ARRAY

我的傑森...

[
{
    "id": "1",
    "name": "Invamore",
    "sub_category": [
        {
            "id": "101",
            "name": "Banner"
        },
        {
            "id": "102",
            "name": "3 sided Dangler"
        },
        {
            "id": "103",
            "name": "Leaflet"
        }
    ]
},
{
    "id": "2",
    "name": "HUPS",
    "sub_category": [
        {
            "id": "103",
            "name": "Leaflet"
        },
        {
            "id": "104",
            "name": "Posters"
        },
        {
            "id": "105",
            "name": "Sunpack"
        }
    ]
},
{
    "id": "3",
    "name": "Xplore",
    "sub_category": [
        {
            "id": "101",
            "name": "Banner"
        },
        {
            "id": "103",
            "name": "Leaflet"
        },
        {
            "id": "106",
            "name": "Dangler"
        },
        {
            "id": "107",
            "name": "Vertical Streamer"
        }
    ]
},
{
    "id": "4",
    "name": "Xpress",
    "sub_category": [
        {
            "id": "101",
            "name": "Banner"
        },
        {
            "id": "103",
            "name": "Leaflet"
        },
        {
            "id": "108",
            "name": "Streamer"
        }
    ]
},
{
    "id": "5",
    "name": "Matrix",
    "sub_category": [
        {
            "id": "103",
            "name": "Leaflet"
        }
    ]
},
{
    "id": "6",
    "name": "Instabrite",
    "sub_category": [
        {
            "id": "103",
            "name": "Leaflet"
        }
    ]
},
{
    "id": "7",
    "name": "Mileage",
    "sub_category": [
        {
            "id": "107",
            "name": "Vertical Streamer"
        }
    ]
},
{
    "id": "8",
    "name": "Onam Posm",
    "sub_category": [
        {
            "id": "101",
            "name": "Banner"
        },
        {
            "id": "106",
            "name": "Dangler"
        }
    ]
}]

后端程序

public void pos_func(String user_id) {

    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);      

    Call call = apiService.POS_MODEL_CALL(user_id);
    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {

        }

        @Override
        public void onFailure(Call call, Throwable t) {
            Log.d("sk_log", "Failed! Error = " + t.getMessage());

        }
    });

}

ApiInterface.java

 @FormUrlEncoded
@POST("pos_distributed.php")
Call<ValuesPos> POS_MODEL_CALL(@Field("user_id") String user_id);

模型:來自pojoschema2pojo.com以獲取上述JSON

ValuesPos.java

package com.example.shkhan.myapp.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shkhan on 14/11/16.
 */
public class ValuesPos {
    @SerializedName("id")
    @Expose
    public String id;
    @SerializedName("name")
    @Expose
    public String name;
    @SerializedName("sub_category")
    @Expose
    public List<SubCategory> subCategory = new ArrayList<SubCategory>();

    /**
     *
     * @return
     * The id
     */
    public String getId() {
        return id;
    }

    /**
     *
     * @param id
     * The id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The name
     */
    public String getName() {
        return name;
    }

    /**
     *
     * @param name
     * The name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     *
     * @return
     * The subCategory
     */
    public List<SubCategory> getSubCategory() {
        return subCategory;
    }

    /**
     *
     * @param subCategory
     * The sub_category
     */
    public void setSubCategory(List<SubCategory> subCategory) {
        this.subCategory = subCategory;
    }

}

SubCategory.json

package com.example.shkhan.myapp.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/**
 * Created by shkhan on 14/11/16.
 */


public class SubCategory {

    @SerializedName("id")
    @Expose
    public String id;
    @SerializedName("name")
    @Expose
    public String name;

    /**
     *
     * @return
     * The id
     */
    public String getId() {
        return id;
    }

    /**
     *
     * @param id
     * The id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The name
     */
    public String getName() {
        return name;
    }

    /**
     *
     * @param name
     * The name
     */
    public void setName(String name) {
        this.name = name;
    }

}

Json包含ValuesPos列表

ApiInterface.java調用單個ValuesPos


編輯

更改ApiInterface.java以調用ValuesPos列表

@FormUrlEncoded
@POST("pos_distributed.php")
Call<List<ValuesPos>> POS_MODEL_CALL(@Field("user_id") String user_id);

這是對我有用的完整代碼...(感謝您的幫助)

改裝二班

ApiInterface.java

@FormUrlEncoded
@POST("pos_distributed.php")
Call<List<ValuesPos>> POS_MODEL_CALL(@Field("user_id") String user_id);

后端程序

public void pos_func(String user_id) {
        dataArrayList1 = new ArrayList<>();
        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);


        Call <List<ValuesPos>> call = apiService.POS_MODEL_CALL(user_id);

        call.enqueue(new Callback<List<ValuesPos>>() {
            @Override
            public void onResponse(Call<List<ValuesPos>> call, Response<List<ValuesPos>> response) {
                Log.d("sk_log", "Status POS Code = successsss");
                dataArrayList1 = response.body();

                //ValuesPos valuesPos = (ValuesPos) response.body();
                Log.d("sk_log", "name==="+dataArrayList1.get(0).getName());
                Log.d("sk_log", "name==="+dataArrayList1.get(0).getSubCategory().get(0).getName());
                CustomerCollection.spinner_pos(dataArrayList1);


            }
            @Override
            public void onFailure(Call<List<ValuesPos>> call, Throwable t) {
                Log.d("sk_log", "Failed! Error = " + t.getMessage());               

            }
        });      
    }

:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM