简体   繁体   English

如何在Android中使用翻新来检索JSON响应

[英]How to retrieve json response using retrofit in android

I am accessing data from a 3rd party api. 我正在从第三方API访问数据。 It's giving this response. 它给出了这个回应。
This is the 3rd party api 这是第三方API

I am unable to get response in the textview. 我无法在textview中得到答复。 Please suggest me some solution. 请给我一些解决方案。 Is this the correct way of writing e.setText(arg0.get(1).getProductBaseInfo() .getProductAttributes().getTitle().toString()); 这是编写e.setText(arg0.get(1).getProductBaseInfo().getProductAttributes()。getTitle()。toString())的正确方法吗?

   {
        "productInfoList": [{
            "productBaseInfo": {
                "productIdentifier": {
                    "productId": "MOBDP6W6MCUWCFGV",
                    "categoryPaths": {
                        "categoryPath": [[{
                            "title": "Mobiles & Accessories"
                        },
                        {
                            "title": "Mobiles"
                        }]]
                    }
                },
                "productAttributes": {
                    "title": "Sony Xperia C Black",

                }
            }]
        }

    public class productInfoList {

    private productBaseInfo productBaseInfo;
    private productShippingBaseInfo productShippingBaseInfo;
    private String offset;

    public productBaseInfo getProductBaseInfo() {
        return productBaseInfo;
    }

    public void setProductBaseInfo(productBaseInfo productBaseInfo) {
        this.productBaseInfo = productBaseInfo;
    }

    public productShippingBaseInfo getProductShippingBaseInfo() {
        return productShippingBaseInfo;
    }

    public void setProductShippingBaseInfo(
            productShippingBaseInfo productShippingBaseInfo) {
        this.productShippingBaseInfo = productShippingBaseInfo;
    }

    public String getOffset() {
        return offset;
    }

    public void setOffset(String offset) {
        this.offset = offset;
    }

}

public class productBaseInfo {

    private productIdentifier productIdentifier;
    private productAttributes productAttributes;

    public productIdentifier getProductIdentifier() {
        return productIdentifier;
    }

    public void setProductIdentifier(productIdentifier productIdentifier) {
        this.productIdentifier = productIdentifier;
    }

    public productAttributes getProductAttributes() {
        return productAttributes;
    }

    public void setProductAttributes(productAttributes productAttributes) {
        this.productAttributes = productAttributes;
    }

}

public class productAttributes {

    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

    public interface Flipkart_Interface {


    @Headers({ "Fk-Affiliate-Token:asdf",
    "Fk-Affiliate-Id:weforteafsd1g" })
    @GET("/search/json?query=sony&resultCount=10")
    void search(Callback<List<productInfoList>> callback);

}

public class MainActivity extends ActionBarActivity {

    List<productInfoList> flip = new ArrayList<productInfoList>();
    TextView e;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e = (TextView) findViewById(R.id.sear);
        String ENDPOINT = "https://affiliate-api.flipkart.net/affiliate";

        RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ENDPOINT)
                .build();
        /*
         * productBaseInfo api1 = adapter.create(productBaseInfo.class);
         * productInfoList api2 = adapter.create(productInfoList.class);
         * productAttributes api3 = adapter.create(productAttributes.class);
         */
        Flipkart_Interface api = adapter.create(Flipkart_Interface.class);

        api.search(new Callback<List<productInfoList>>() {

            @Override
            public void success(List<productInfoList> arg0, Response arg1) {
                // TODO Auto-generated method stub
                e.setText(arg0.get(1).getProductBaseInfo()
                        .getProductAttributes().getTitle().toString());

            }

            @Override
            public void failure(RetrofitError arg0) {
                // TODO Auto-generated method stub

            }
        });

    }
}

I am unable to get response in the textview. 我无法在textview中得到答复。 Please suggest me some solution. 请给我一些解决方案。 Is this the correct way of writing e.setText(arg0.get(1).getProductBaseInfo() .getProductAttributes().getTitle().toString()); 这是编写e.setText(arg0.get(1).getProductBaseInfo().getProductAttributes()。getTitle()。toString())的正确方法吗?

Looking at the JSON, you have a one element List of productInfoList elements, and you're accessing index 1. 查看JSON,您将获得一个productInfoList元素的单元素List ,并且正在访问索引1。

Try accessing index zero, and make sure that your List has data: 尝试访问索引零,并确保您的List包含数据:

@Override
        public void success(List<productInfoList> arg0, Response arg1) {
            // TODO Auto-generated method stub
            Log.d("MyApp", "arg0 size: " + arg0.size());

            //make sure the List has data
            if (arg0.size() > 0){
               //log the value
               Log.d("MyApp", "Title Value: " + arg0.get(0).getProductBaseInfo().getProductAttributes().getTitle().toString());
               e.setText(arg0.get(0).getProductBaseInfo()
                    .getProductAttributes().getTitle().toString());
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用改型从API的JSON响应中检索图像 - How to retrieve image from a JSON Response from API using retrofit 在Android上使用翻新的JSON响应 - JSON Response Using Retrofit on Android 如何使用 MPAndroidChart 和 retrofit 检索 JSON 数据和 plot 折线图 ZC31B32364CE1ECCEAZFCD - How to retrieve JSON data and plot a LineChart using MPAndroidChart and retrofit on android Android:如何使用改型从json响应中获取特定字符串? - Android: How to get specific string from json response using retrofit? 如何在Android中使用改造在Json响应中获取数组数据? - How to get array data in Json response using retrofit in android? 如何使用RETROFIT解析此JSON响应并在Recylcer View Android中填充 - How to parse this JSON response using RETROFIT and populate in Recylcer view Android 如何使用 Android 中的 retrofit 从这种类型的 JSON 响应中获取数据 - How to get data from this type of JSON response using retrofit in Android 如何使用改造将 json 响应添加到 android 中的微调器中? - How to add the json response into spinner in android using retrofit? android:如何使用 Retrofit 接收来自以下 json 的响应? - android: How to receive response from the following json using Retrofit? 使用 Retrofit Android 解析响应中的 JSON 数组 - Parse JSON Array in Response using Retrofit Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM