0

I want to get information from baidu music web api. I use below tow link: http://openapi.baidu.com/rest/2.0/music/billboard/billlist?page_size=50&timestamp=2016-09-22+18%3A47%3A54&sign=b2e34bd4b6c19a065d5a7e49e591a41b&session_key=9mzdDcHtDENrxP3Spvk7ZFbNHNijT8l8fN%2BfI%2Fe3U5rh3U5g%2FDVvjZyqB48aJaYO5qaqw4JbugF79hgAQ%2FGBIixUSuaP&type=21&page_no=1

http://openapi.baidu.com/rest/2.0/music/billboard/billlist?page_size=50&timestamp=2016-09-26+17%3A42%3A18&sign=62cc904c8ff2817280f699a4bd4b6496&session_key=9mzdDcHtDENrxP3Spvk7ZFbNHNijT8l8fN%2BfI%2Fe3U5rh3U5g%2FDVvjZyqB48aJaYO5qaqw4JbugF79hgAQ%2FGBIixUSuaP&type=23&page_no=1

only type is different. one is 23 and another is 21. For return jason: one is billboard21, another is billboard23. If I want to use retrofit2 to accessa api and parse return jason, should I define two different java class to handler this two different return? In face, there is about 17 type, how can I handle this case if don't want to create 17 java classes?

my access api is:

@GET("/rest/2.0/music/billboard/billlist")
Call<BaiduBillListContainer> getBillList(@Query("timestamp") String timestamp,
                                         @Query("type") String type,
                                         @Query("sign") String sign,
                                         @Query("session_key") String sessionkey,
                                         @Query("page_size") String pagesize,
                                         @Query("page_no") String pageno); 

BaiduBillListContainer.java define:

public class BaiduBillListContainer implements Serializable {
    BaiduBillList billboard21;

    public BaiduBillList getBillBoardInfo() {
        return billboard21;
    }

    public void setBillBoardInfo(BaiduBillList billBoardInfo) {
        this.billboard21 = billBoardInfo;
    }
}

it cannot used to get billboard23 return. Any one can help me about this question? thanks very much.

SripadRaj
  • 1,687
  • 2
  • 22
  • 33
mmm2006
  • 1,227
  • 2
  • 11
  • 18
  • Did you created local DB for this? If Yes you can create one main table and different sub tables. Main Table Contains all the information. sub table contains their Unique Id's and type. So you have to create only one class for all the details and 17 sub classes with unique_id and type – Naveen Kumar M Sep 27 '16 at 05:49
  • You have to declare different java classes in order to parse different json keys – Julián Martínez Sep 27 '16 at 06:04
  • You can receive jsonObject(using retrofit) and dynamically convert it to array of pojo after getting jsonArray from it. You can checkout this answer(http://stackoverflow.com/questions/24279245/how-to-handle-dynamic-json-in-retrofit#answer-28576252) – Fabin Paul Sep 27 '16 at 07:04

1 Answers1

0

Try this:

public class BaiduBillListContainer implements Serializable {
    Map<String,BaiduBillList> billboard;

    public Map<String,BaiduBillList> getBillBoardInfo() {
        return billboard;
    }

    public void setBillBoardInfo(Map<String,BaiduBillList> billBoardInfo) {
        this.billboard= billBoardInfo;
    }
}

Then call billboard from map like:

Map<String,BaiduBillList> getBBData=response.body();

BaiduBillList receivedBBList=getBBData.values().toArray()[0]
Rushi M Thakker
  • 679
  • 2
  • 15
  • 34