Chuyển đến nội dung chính

Bài đăng

Đang hiển thị bài đăng từ Tháng 6, 2019

Parse JSON array response using Retrofit & Gson

@FormUrlEncoded @POST ( "api/sponsors" ) Call < List < SponsorsResult >> getStatesAndDistrict ( @Field ( "xyz" ) String field1 ); Call < List < SponsorsResult >> call = service . getSponsorsValue (); call . enqueue ( new Callback < List < SponsorsResult >>() { @Override public void onResponse ( Call < List < SponsorsResult >> call , Response < List < SponsorsResult >> response ) { List < SponsorsResult > rs = response . body (); } @Override public void onFailure ( Call < List < SponsorsResult >> call , Throwable t ) { } }); class SponsorsResult { @SerializedName ( "sponsors" ) private List < SponsorsValue > sponsors ; public List < SponsorsValue > getSponsors () { return sponsors ; } } ...

Using Retrofit 2.x as REST client - Tutorial

https://www.vogella.com/tutorials/Retrofit/article.html 1. Retrofit 1.1. What is Retrofit Retrofit is a REST Client for Java and Android. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice. In Retrofit you configure which converter is used for the data serialization. Typically for JSON you use GSon, but you can add custom converters to process XML or other protocols. Retrofit uses the OkHttp library for HTTP requests. You can generate Java objects based on JSON using the following tool:  http://www.jsonschema2pojo.org/  This can be useful to create complex Java data structures from existing JSON. 1.2. Using Retrofit To work with Retrofit you basically need the following three classes: Model class which is used as a JSON model Interfaces that define the possible HTTP operations Retrofit.Builder class - Instance which uses the interface and the Builder API to allow defining the URL...