I am fairly new to Android development and I am creating an app that requires me to consume the Zomato Rest API. I am using the Koush ION library https://github.com/koush/ion for sending the http request and receiving a response. For using this library, I simply create a model java class with the keys as java instance variables and call this line of code.
Ion.with(getActivity())
.load("https://developers.zomato.com/api/v2.1/geocode?lat=25.12819&lon=55.22724")
.setHeader("user-key",my-user-key)
.asJsonArray()
.setCallback(new FutureCallback<JsonArray>() {
@Override
public void onCompleted(Exception e, JsonArray result) {
if (e != null) {
Log.d("FoodFragment", "Error loading food"+e.toString());
Toast.makeText(getActivity(), "error loading food", Toast.LENGTH_LONG).show();
return;
}
else{
Log.d("FoodFragment", "Size= " + result.size() + "; " + result.toString());
sTrips = (List<RestaurantsZomato>) new Gson().fromJson(result.toString(), new TypeToken<List<RestaurantsZomato>>() {
}.getType());
adapter = new RestaurantAdapter(sTrips);
recyclerView.setAdapter(adapter);
mSwipeRefreshLayout.setRefreshing(false);
((RestaurantAdapter) adapter).setOnItemClickListener(new RestaurantAdapter.MyClickListener() {
@Override
public void onItemClick(int position, View v) {
Toast.makeText(getActivity(), "Item Clicked", Toast.LENGTH_LONG).show();
}
});
}
}
});
}
As per the Zomato API documentation, the response will look something like this -
{"nearby_restaurants": {
"1": {
"restaurant": {
}
},
"2": {
"restaurant": {
}
},
"3": {
"restaurant": {
}
},
"4": {
"restaurant": {
}
},
"5": {
"restaurant": {
}
},
"6": {
"restaurant": {
}
},
"7": {
}
},
"8": {
"restaurant": {
}
},
"9": {
"restaurant": {
}
}
And my RestaurantZomato class looks like this -
public class RestaurantsZomato {
public NearbyRestaurants nearby_restaurants;
public static class NearbyRestaurants {
public List<RestaurantDetails> restaurant;
}}
And RestaurantDetails class has everything inside the "restaurant" tag. My question how to represent "1", "2", "3" etc that is present in the JsonResponse in my Model class.
I'm sorry if this question is stupid. As I said I'm new to Android development and any resources that point me in the right direction will be much appreciated!
public List< RestaurantDetails> restaurant; return Array
public RestaurantDetails restaurant; return Object
Your API return Object. Not return Array
Try this!!
Update
Your Json String return JsonObject. You must use JsonObject