如何在android中使用retrofit检索json响应

时间:2015-04-08 17:50:22

标签: android json retrofit

我正在访问来自第三方API的数据。它给出了这个回应 这是第三方api

我无法在textview中得到回复。 请给我一些解决方案。 这是正确的写作方式吗?     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

            }
        });

    }
}

我无法在textview中得到回复。 请给我一些解决方案。 这是正确的写作方式吗?     e.setText(arg0.get(1).getProductBaseInfo()                         。.getProductAttributes()的getTitle()的toString());

1 个答案:

答案 0 :(得分:0)

查看JSON,您有List个元素productInfoList,并且您正在访问索引1.

尝试访问索引零,并确保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());
            }

        }