I am trying to make an Android class that creates a List of other apps, for later display, until now I have the following code for creating the List (taken from a google example):
public final class AppList {
public static List<App> list;
public static List<App> setupApp() {
list.add(buildAppInfo(
"Browsers",
"chrome",
"description",
"Google Inc.",
"com.android.chrome",
"http://...",
"http://..."
));
list.add(buildAppInfo(
"Browsers",
"Firefox",
"description",
"Mozilla",
"com.mozilla.firefox",
"http://...",
"http://..."
));
[An so on...]
return list;
}
private static App buildAppInfo(String category,
String title,
String description,
String developer,
String packageName,
String iconImageUrl,
String bgImageUrl) {
App app = new App();
app.setId(App.getCount());
App.incCount();
app.setTitle(title);
app.setDescription(description);
app.setDeveloper(developer);
app.setCategory(category);
app.setIconImageUrl(iconImageUrl);
app.setBackgroundImageUrl(bgImageUrl);
app.setPackageName(packageName);
return app;
}
}
//From here everything is handled in other activity
I will like instead to load the details dynamically from a web server that I can setup to feed either a JSONArray or an XML and then put that data on the list. For what I was reading I could use Volley, but I am not very experienced in Volley or Java in general (I am just learning in the road) so I don't have to much idea on how to proceed.
From the google developers site I found an example. I was thinking on add something like this:
public final class AppList {
public static List<App> list;
public static List<App> setupApp() {
/*Json Request*/
String url = "https://json_url/";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
list.add(response);//ADD ITEMS TO LIST HERE
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
return list;
}
private static App buildAppInfo(String category,
String title,
String description,
String developer,
String packageName,
String iconImageUrl,
String bgImageUrl) {
App app = new App();
app.setId(App.getCount());
App.incCount();
app.setTitle(title);
app.setDescription(description);
app.setDeveloper(developer);
app.setCategory(category);
app.setIconImageUrl(iconImageUrl);
app.setBackgroundImageUrl(bgImageUrl);
app.setPackageName(packageName);
return app;
}
}
The data received from the server feed should be something like:
buildAppInfo(
"Browsers",
"Firefox",
"description",
"Mozilla",
"com.mozilla.firefox",
"http://...",
"http://..."
)
buildAppInfo(
"Browsers",
"Firefox",
"description",
"Mozilla",
"com.mozilla.firefox",
"http://...",
"http://..."
)
[...]
//BUT IN JSON FORMAT
My entire approach obviously doesn't work. Can someone please help me to achieve what I need? Thank you.