How to consume this JSON structure via Retrofit 2?

前端 未结 2 812
予麋鹿
予麋鹿 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条回答
  •  -上瘾入骨i
    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> 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> 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

提交回复
热议问题