使用Retrofit从最里面的类中获取数据.帮我显示A类和B类的数据 [英] fetching data from innermost class using Retrofit. Help me to display data of classes A and B

查看:80
本文介绍了使用Retrofit从最里面的类中获取数据.帮我显示A类和B类的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 以下是我尝试从中获取数据的JSON.我需要
  2. 使用Retrofit2提取并显示名称和类. 3.我尝试了几个站点,但是只能直接从数组中获取数据 4.但不是来自对象数组.这将由该代码构成.

  1. The following is the JSON I was trying to fetch data from. I need to
  2. extract and display names and class using retrofit2. 3.I tried several sites, but only able to fetch data directly from an array 4.But not from an array of objects. That would be formed by this code.

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

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

您能告诉我为什么我要使用onFailure()方法吗?我的代码有什么问题

Can you tell me Why I am getting into onFailure() method. What is wrong with my code

     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

The following is 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数据映射到模型.

Your JSON is invalid. But, for your question, the answer is You have to create models using http://www.jsonschema2pojo.org/ from JSON and after that use GSON library to map json data to models.

引用此链接,您将获得一个主意 https://guides.codepath .com/android/Consuming-APIs-with-Retrofit

Refer this link you will get the idea https://guides.codepath.com/android/Consuming-APIs-with-Retrofit

对于上面的示例,您将获得名称&像

For above example, you will get the name & class like

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类做同样的事情

这篇关于使用Retrofit从最里面的类中获取数据.帮我显示A类和B类的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆