0

I am trying to setup an Android List that gets information from an JSON array on the internet, the JSON is this:

[
{
    "id":1,
    "category":"Category1",
    "title":"Title1",
    "description":"description1",
    "studio":"studio1",
    "videoUrl":"https://example.com",
    "cardImageUrl":"http://example.com",
    "bgImageUrl":"http://example.com"},
{
    "id":2,
    "category":"category2",
    "title":"title2",
    "description":"description2",
    "studio":"studio2",
    "videoUrl":"http://example.com",
    "cardImageUrl":"http://example.com",
    "bgImageUrl":"http://example.com"
}
]

This is my code used to retrieve the data from the server:

public static List<Movie> setupMovie()
{
    List<Movie> list = new ArrayList<>();

    try {
        URL data = new URL("http://example.com/data.php");
        URLConnection tc = data.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                tc.getInputStream()));

        String line = null;
        JSONArray array = new JSONArray(line);
        while ((line = in.readLine()) != null) {
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.getJSONObject(i);
                list.add(buildAppInfo(object.getString("category"), object.getString("title"), object.getString("description"),
                        object.getString("studio"), object.getString("videoUrl"), object.getString("cardImageUrl"),
                        object.getString("bgImageUrl")));
            }
        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return list;
}

When I run the APK in android Studio it crashes with the following error:

Process: com.example.myapp, PID: 8034
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.MainActivity}: android.os.NetworkOnMainThreadException
Application terminated.

I know the answer is to do an Async Task, I tried reading other answers on similar questions, but I am still don't understand how to correctly setup an Async Task on my case. I also tried the StrictMode but it does not work.

My main problem is that I don't understand how to setup async task work on my project. Any help will be very much appreciated, thank you.

Valerie Castle
  • 113
  • 1
  • 11
  • you have to do this in a thread or use AsyncTask or any other tools like okhttp,Volley,Retrofit – Rushi Ayyappa Nov 28 '16 at 08:50
  • I was typing an answer but the post got locked too fast. Currently, you are trying to fetch data on the main (or UI) thread. The reason Android blocks this is because it freezes up your UI until the fetching of the data is done. You can understand that for large data sets this may result in an application being disabled for a couple seconds, which is really bad from an usuability standpoint. Android's built in AsyncTask is probably the easiest to implement in your situation. This thread discusses some of the pros/cons: http://stackoverflow.com/questions/6964011/handler-vs-asynctask-vs-thread – nbokmans Nov 28 '16 at 08:50
  • @valerie http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html#example-asynctask – Rajesh Nov 28 '16 at 08:50

0 Answers0