问题
In the json below i am able to read the fields in "categories" using the following android code. I hava no idea how to read the "effect_list" elements "4" and "1" . The "4" and "1" are incremental and Dynamic. How should i create a pojo class for this and how my code should be in Main_get.java ?
{
"categories":[
{
"mcategory_id":"4",
"mcategory_name":"Band"
},
{
"mcategory_id":"1",
"mcategory_name":"Basic Effects"
},
{
"mcategory_id":"3",
"mcategory_name":"Bg Image Card"
}
],
"effect_list":{
"4":[
{
"effects_id":"18",
"effects_name":"Band 1"
},
{
"effects_id":"19",
"effects_name":"Band 2"
}
],
"1":[
{
"effects_id":"1",
"effects_name":"Background Blur"
},
{
"effects_id":"4",
"effects_name":"Blemish Removal"
}
]
}
}
I am using the following code to get json Array "categories" using retrofit and its working without any problem. Now how can i get the "effect_list" fields?
Contact.java
public class Contact {
@SerializedName("mcategory_id")
@Expose
private String mcategory_id;
@SerializedName("mcategory_name")
@Expose
private String mcategory_name;
public String getmcategory_id() {
return name;
}
public void setmcategory_id(String name) {
this.name = name;
}
public String getmcategory_name() {
return email;
}
public void setmcategory_name(String email) {
this.email = email;
}
}
ApiService.java
public interface ApiService {
@GET("xyz.json")
Call<ContactList> getMyJSON();
}
ContactList.java
public class ContactList {
@SerializedName("contacts")
@Expose
public ArrayList<Contact> contacts = new ArrayList<>();
/**
* @return The contacts
*/
public ArrayList<Contact> getContacts() {
return contacts;
}
/**
* @param contacts The contacts
*/
public void setContacts(ArrayList<Contact> contacts) {
this.contacts = contacts;
}
}
RetroClient.java
public class RetroClient {
private static final String ROOT_URL = "http://abc.ab/";
private static Retrofit getRetrofitInstance() {
return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
/**
* Get API Service
*
* @return API Service
*/
public static ApiService getApiService() {
return getRetrofitInstance().create(ApiService.class);
}
}
MyContactAdapter.java
List<Contact> contactList;
Context context;
private LayoutInflater mInflater;
// Constructors
public MyContactAdapter(Context context, List<Contact> objects) {
super(context, 0, objects);
this.context = context;
this.mInflater = LayoutInflater.from(context);
contactList = objects;
}
@Override
public Contact getItem(int position) {
return contactList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
vh = ViewHolder.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
Contact item = getItem(position);
vh.textViewName.setText(item.getName());
vh.textViewEmail.setText(item.getEmail());
// Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView);
Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView);
// loading.dismiss();
return vh.rootView;
}
public static class ViewHolder {
public final RelativeLayout rootView;
public final ImageView imageView;
public final TextView textViewName;
public final TextView textViewEmail;
public ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
this.rootView = rootView;
this.imageView = imageView;
this.textViewName = textViewName;
this.textViewEmail = textViewEmail;
}
public static ViewHolder create(RelativeLayout rootView) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
return new ViewHolder(rootView, imageView, textViewName, textViewEmail);
}
}
Main_get.java
public class Main_get extends AppCompatActivity {
/**
* Views
*/
private GridView listView;
private View parentView;
private ArrayList<Contact> contactList;
private MyContactAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Array List for Binding Data from JSON to this List
*/
contactList = new ArrayList<>();
parentView = findViewById(R.id.parentLayout);
/**
* Getting List and Setting List Adapter
*/
listView = (GridView) findViewById(R.id.listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Snackbar.make(parentView, contactList.get(position).getName() + " => " + contactList.get(position).getName().getBytes(), Snackbar.LENGTH_LONG).show();
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
assert fab != null;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(@NonNull final View view) {
//Creating an object of our api interface
ApiService api = RetroClient.getApiService();
/**
* Calling JSON
*/
Call<ContactList> call = api.getMyJSON();
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<ContactList>() {
@Override
public void onResponse(Call<ContactList> call, Response<ContactList> response) {
if(response.isSuccessful()) {
/**
* Got Successfully
*/
contactList = response.body().getContacts();
/**
* Binding that List to Adapter
*/
adapter = new MyContactAdapter( Main_get.this, contactList);
listView.setAdapter(adapter);
} else {
// Snackbar.make(parentView, R.string.string_some_thing_wrong, Snackbar.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<ContactList> call, Throwable t) {
dialog.dismiss();
}
});
}
});
}
}
What are all the changes i should do in this code?
回答1:
You can use
Map<Integer, List<Effect>>
In Integer you'll get index ("1", "4"...) and in List you'll get array of effects.
来源:https://stackoverflow.com/questions/41183936/dynamic-multiple-json-array-in-android-retrofit-2