Import data from a JSON or XML feed and create a list on Android

I'm trying to create an Android class that creates a list of other applications for later display, so far I have the following code to create a 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

Instead, I would like to dynamically load data from a web server, which I can configure to feed either JSONArray or XML, and then put that data into a list. For what I read, I could use Volley, but I am not very experienced in Volley or Java at all (I just learned on the road), so I don’t need to think much about how to proceed.

I found an example from the Google Developers site . I was thinking of adding 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 look something like this:

buildAppInfo(
            "Browsers",
            "Firefox",
            "description",
            "Mozilla",
            "com.mozilla.firefox",
            "http://...",
            "http://..."
    )
buildAppInfo(
            "Browsers",
            "Firefox",
            "description",
            "Mozilla",
            "com.mozilla.firefox",
            "http://...",
            "http://..."
    )
[...]
//BUT IN JSON FORMAT

, , . -, , , ? .

+4
1

, , :

URL-, JSON. , JSONArray JSONObject ( mvnrepository):

public static JSONArray parseJsonFromUrl(String uri) throws IOException, JSONException {
    JSONArray array = null;
    try (Scanner sn = new Scanner(new URL(uri).openStream(), "UTF-8")){
      array = new JSONArray(sn.useDelimiter("\\A").next());
    }
    return array;
}

List App, :

public static List<App> getAppList(JSONArray array){
    List<App> appList = new ArrayList<>();
    for (int i = 0; i < array.length(); i++) {
            JSONObject object = array.getJSONObject(i);
            appList.add(buildAppInfo(object.getString("category"), object.getString("title"), object.getString("description"), object.getString("developer"), object.getString("packageName"), object.getString("iconImageUrl"), object.getString("bgImageUrl")));
    }
    return appList;
}

XML , XML (. XML URL- java?).

, Jackson Mapper Spring RestTemplate RESTful, , Java.

[]

, , . , Spring Boot. ( https://elcodedocle-jsontest.herokuapp.com/apps travisCI )

[ANOTHER_UPDATE]

, , Android, (json- "jon- " ), RESTful, , Android (, Android): https://github.com/elcodedocle/android-resttest

, , , . Java, , , , .

+2

Source: https://habr.com/ru/post/1662012/


All Articles