How to consume this JSON structure via Retrofit 2?

前端 未结 2 811
予麋鹿
予麋鹿 2020-12-12 06:28

Hello I have a json structure and I need to get the datas from url. How can I do that? What are classes and functions. Please help me. Thanks.

here is my json.

相关标签:
2条回答
  • 2020-12-12 06:44

    http://www.jsonschema2pojo.org/

    Put your JSON response here and download java code from this website . Then put this java code into your project and use it . This website will make all required classes with all required fields which will be perfect fit for your response of JSON. I have used it many time this will surely work.

    http://square.github.io/retrofit/ here you can get detailed information about how to use retrofit to POST or GET data .

    Retrofit turns your HTTP API into a Java interface.

    public interface YourService {
      @GET("users/{user}/repos")
      Call<List<Repository>> listRepos(@Path("user") String user);
    }
    

    The Retrofit class generates an implementation of the interface.

    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://api.yourAPI.com/")
        .build();
    
    YourService  service = retrofit.create(YourService .class);
    
    Call<List<Repository>> repos = service.listRepos("myrepository");
    

    Use annotations to describe the HTTP request:

    • URL parameter replacement and query parameter support Object conversion to request body (e.g., JSON, protocol buffers) Multipart request body and file upload
    0 讨论(0)
  • 2020-12-12 06:55

    Use jsonschema2pojo.org Paste you responce and it will generate Java classes for you. You instantiate Retrofit instance by setting all necessary components like this:

    Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BuildConfig.BASE_URL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
    

    and your interfaces will look like:

    @GET("api/service/schedule/{filial}")
        Observable<Response<List<Event>>> getSchedule(@Path("filial") String filial);
    

    where @GET- annotation defining request type, Response<> - type used by Retrofit, carrying information whether response is successful or not (see class methods). Observable<> - type from rxJava library, allowing to process request in reactive way. I strongly recommend to use RxJava, because it simplifies execution in background very much. (DON't use AsyncTasks!!).

    in order to use Retrofit2 add following lines to your Gradle file:

       //Reactive programming
        compile 'io.reactivex:rxjava:1.1.5'
        compile 'io.reactivex:rxandroid:1.2.0'
    
    
        compile 'com.google.code.gson:gson:2.4'
        //retrofit and okhttp
        compile 'com.squareup.okhttp3:okhttp:3.2.0'
        compile 'com.squareup.retrofit2:retrofit:2.0.2'
        compile 'com.squareup.retrofit2:converter-gson:2.0.2'
        compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
    

    You will instantiate Retrofit2 interface by a similar call: retrofit.create(Api.class)

    0 讨论(0)
提交回复
热议问题