-1

I tried to parse some url from json data to get only the image from the url inside the json and show it as a picture. But I keep getting errors from it and i don't know how to solve it (a.k.a I'm still a noob at this).

I'm using the code from here and change the json file and the code a bit.

This is the code

public class MainActivity extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();

// Movies json url
static final String url = "http://url";
ProgressDialog pDialog;
List<Movie> movieList = new ArrayList<Movie>();
ListView listView;
CustomListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListAdapter(this, movieList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    // changing action bar color
    getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#1b1b1b")));

    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            Movie movie = new Movie();
                            JSONObject obj = response.getJSONObject(i);
                            movie.setTitle(obj.getString("title"));

                            //here where I try to parse the string from json file 
                            String url2= obj.getString("image");
                            Document document = Jsoup.connect(url2).get();
                            Elements img = document.select("div [class=ipsBox] img[src]");
                            String imgSrc = img.attr("src");

                            movie.setThumbnailUrl(imgSrc);

                            movie.setRating(((Number) obj.get("rating"))
                                    .doubleValue());
                            movie.setYear(obj.getInt("releaseYear"));

                            // Genre is json array
                            JSONArray genreArry = obj.getJSONArray("genre");
                            ArrayList<String> genre = new ArrayList<String>();
                            for (int j = 0; j < genreArry.length(); j++) {
                                genre.add((String) genreArry.get(j));
                            }
                            movie.setGenre(genre);


                            // adding movie to movies array
                            movieList.add(movie);


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

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }
            });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(movieReq);
}


@Override
public void onDestroy() {
    super.onDestroy();
    hidePDialog();
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}

this is the json i used

[{
    "title": "Dawn of the Planet of the Apes",
    "image": "http://bato.to/comic/_/comics/000000-ultra-black-r1425",
    "rating": 8.3,
    "releaseYear": 2014,
    "genre": ["Action", "Drama", "Sci-Fi"]
},
{
    "title": "District 9",
    "image": "http://bato.to/comic/_/comics/g-edition-r886",
    "rating": 8,
    "releaseYear": 2009,
    "genre": ["Action", "Sci-Fi", "Thriller"]
},
{
    "title": "Transformers: Age of Extinction",
    "image": "http://otherurl",
    "rating": 6.3,
    "releaseYear": 2014,
    "genre": ["Action", "Adventure", "Sci-Fi"]
},
{
    "title": "X-Men: Days of Future Past",
    "image": "http://otherurl",
    "rating": 8.4,
    "releaseYear": 2014,
    "genre": ["Action", "Sci-Fi", "Thriller"]
},
{
    "title": "How to Train Your Dragon",
    "image": "http://otherurl",
    "rating": 8.2,
    "releaseYear": 2010,
    "genre": ["Animation", "Adventure", "Family"]
}]

And after I run the program it come with errors

The Error I get

12-13 21:54:55.701: E/AndroidRuntime(28628): FATAL EXCEPTION: main
12-13 21:54:55.701: E/AndroidRuntime(28628): Process: info.androidhive.customlistviewvolley, PID: 28628
12-13 21:54:55.701: E/AndroidRuntime(28628): android.os.NetworkOnMainThreadException
12-13 21:54:55.701: E/AndroidRuntime(28628):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:449)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:434)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:181)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:170)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:77)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:1)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at android.os.Handler.handleCallback(Handler.java:733)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at android.os.Looper.loop(Looper.java:136)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at android.app.ActivityThread.main(ActivityThread.java:5146)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at java.lang.reflect.Method.invokeNative(Native Method)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at java.lang.reflect.Method.invoke(Method.java:515)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
12-13 21:54:55.701: E/AndroidRuntime(28628):    at dalvik.system.NativeStart.main(Native Method)

Please help me, I really appreciate your answer.

zerm
  • 3
  • 1
  • your error says `12-13 21:54:55.701: E/AndroidRuntime(28628): android.os.NetworkOnMainThreadException`, so try using AsyncTask for all the network related operations. That will fix it. – Mohit Dec 13 '14 at 14:13
  • I've tried the AsyncTask, and it keep showing errors, thank you for the answer anyway :D – zerm Dec 13 '14 at 14:25

1 Answers1

0

As per this answer, I think you need these two lines before your start parsing:

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
Community
  • 1
  • 1
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
  • Wow, I'm speechless now, it just need these two lines to solve all my problems, Thank you so much! – zerm Dec 13 '14 at 14:24