The following code will help get a nested json object and an array
for example: json
{ "similar_product":[ { ..... } ], "options":{ "Blouse Length":[ { "value_id":"696556", }
First we need to create a model class, the element names of the model class are the same in the json element, which we can use @SerializedName("for exact json name")
public class Product { public Options options; public void setOptions(Options options) { this.options = options; } public Options getOptions() { return options; }
create an interface for modification to get the json element in the URL
// don't need to put values of id in retrofit ex:: "/api-mobile_.php?method=getProductById&pid="
just pass the url parameter in the request, it automatically retrieves the url
eg:
public interface Retrofit_Api { @FormUrlEncoded @GET("/api-mobile_.php?method=getProductById") Call<Product> responseproduct(@Query("pid") String pid); }
In your main class
String pid=editid.getText().toString(); final Retrofit adapter = new Retrofit.Builder() .baseUrl(Product_url) .addConverterFactory(GsonConverterFactory.create()) .build(); //Creating an object of our api interface Retrofit_Api api = adapter.create(Retrofit_Api.class); Call<Product> call = api.responseproduct(pid); call.enqueue(new Callback<Product>() { @Override public void onResponse(Call<Product> call, Response<Product> response) { ArrayList<Product.BlouseLength> p= new ArrayList(response.body().getOptions().getBlouseLengths()); Editadapter editadapter=new Editadapter(MainActivity.this,p); recyclerView.setAdapter(editadapter); } @Override public void onFailure(Call<Product> call, Throwable t) { Log.d("Error", t.getMessage()); } }); }
lemuriyan Aug 17 '16 at 7:33 2016-08-17 07:33
source share