簡體   English   中英

使用Retrofit從最里面的類中獲取數據。 幫我顯示A類和B類的數據

[英]fetching data from innermost class using Retrofit. Help me to display data of classes A and B

  1. 以下是我嘗試從中獲取數據的JSON。 我需要
  2. 使用Retrofit2提取並顯示名稱和類。 3.我嘗試了幾個站點,但只能直接從數組中獲取數據。4.但不能從對象數組中獲取數據。 這將由此代碼形成。

      { "Class1" :{ "A": [ { "name" : "paul", "class" : "first" } ], "B": [ { "name" : "anna", "class" : "second" } ] }, "Class2" :{ "A": [ { "name" : "matthew", "class" : "first" } ], "B": [ { "name" : "joe", "class" : "second" } ] } } 

您能告訴我為什么要進入onFailure()方法嗎? 我的代碼有什么問題

     public class MainActivity extends AppCompatActivity {

private ListView listView;
private static final String Tag = "com.example";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView)findViewById(R.id.bList);

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    String API_BASE_URL = "xyz.com";

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    httpClient.addInterceptor(logging); 
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();



    ApiInterface client =  retrofit.create(ApiInterface.class);

    Call<List<Example>> call = client.getdate();


    call.enqueue(new Callback<List<Example>>() {
        @Override
        public void onFailure(Call<List<Example>> call, Throwable t) {

            Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onResponse(Call<List<Example>> call, Response<List<Example>> response) {

            if (response.isSuccessful()) {
                //Response success. Handle data here
            }
            else{
                //For getting error message
                Log.d("Error message",response.message());
                //For getting error code. Code is integer value like 200,404 etc
                Log.d("Error code",String.valueOf(response.code()));
            }

        }
    });
}

}

例子是我的主班

以下是MyAPIInterface.java

public interface ApiInterface {public interface ApiInterface {
          @GET("/sement")
          Call<List<Example>> getdate(@Query("date")String date);

              }

您的JSON無效。 但是,對於您的問題,答案是您必須使用JSON中的http://www.jsonschema2pojo.org/創建模型,然后使用GSON庫將JSON數據映射到模型。

引用此鏈接,您將獲得想法https://guides.codepath.com/android/Consuming-APIs-with-Retrofit

要么

對於上面的示例,您將獲得名稱和類,例如

List<A> alist = mainclass.getClass1().getA();
for (int i = 0; i < alist.size(); i++) {
       String name = alist.get(i).getName();
       String class = alist.get(i).getClass();
}

對B類也一樣

首先驗證您的Json。

使用此工具讓Json對對象進行分類http://www.jsonschema2pojo.org/

一旦創建對象。

這里的getData()是第一個json對象。

List<A> listA= response.body().getData().getClass1().getA();
List<B> listB=response.body().getData().getClass1().getB();

for(A list:listA)
list.getname();        //paul
list.getclass();       //first

for(B list:listB) 
list.getname();       // anna
list.getclass();      //second



List<A> listA= response.body().getData().getClass2().getA();
List<B> listB=response.body().getData().getClass2).getB();

for(A list:listA)
list.getname();           //matthew
list.getclass();          //first

for(B list:listB)
list.getname();           //joe
list.getclass();          //second

暫無
暫無

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

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