Httpurlconnection returning empty

Asked

Viewed 80 times

1

Message

Attempt to invoke virtual method 'int java.lang.String.length()' on a null Object Reference

Calling for

JSONArray data = new JSONArray(getHttpGet(url));

Code:

public static String getHttpGet(String url) {
        String result = null;
        try {
            URL myurl = new URL("http://www.exemplo.com.br/gerajson.php");
            HttpURLConnection urlconnection = (HttpURLConnection) myurl.openConnection();
            urlconnection.setRequestMethod("GET");
            urlconnection.setDoInput(true);
            urlconnection.connect();
            InputStream is = urlconnection.getInputStream();
            if (is != null) {
                StringBuffer sb = new StringBuffer();
                String line;
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is));
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }
                    reader.close();
                } finally {
                    is.close();
                }
                result = sb.toString();
            }
        } catch (Exception e) {
            result = null;
        }
        return result;
    }

Update (when changing the content of catch):

app has stopped. br.com.webvisionsistemas.shishamaps E/AndroidRuntime: FATAL EXCEPTION: mainProcess: br.com.webvisionsistemas.shishamaps, PID: 2561 java.lang.RuntimeException: android.os.NetworkOnMainThreadException at br.com.webvisionsistemas.shishamaps.MapaFragment.getHttpGet( Mapafragment.java:17 6) at br.com.webvisionsistemas.shishamaps.Mapafragment.onMapReady( Mapafragment.java:64)

  • } catch (Exception e) { result = null; } - This from here is not a good idea, you’re swallowing the real mistake to mask it like a NullPointerException after. If you change this result = null; by a e.printStackTrace(); throw new RuntimeException(e);, what appears?

  • app has stopped. br.com.webvisionsistemas.shishamaps E/Androidruntime: FATAL EXCEPTION: mainProcess: br.com.webvisionsistemas.shishamaps, PID: 2561 java.lang.Runtimeexception: android.os.Networkonmainthreadexception at br.com.webvisionsistemas.shishamaps.Mapafragment.getHttpGet(Mapafragment.java:176) at br.com.webvisionsistemas.shishamaps.Mapafragment.onMapReady(Mapafragment.java:64)

1 answer

1

The first problem is this passage:

    } catch (Exception e) {
        result = null;
    }

This here is not a good idea, because it will swallow the real mistake to mask it as a NullPointerException after. Changing the result = null; therefore:

        e.printStackTrace();
        throw new RuntimeException(e);

The generated exception is different (as per your comment). The exception becomes android.os.NetworkOnMainThreadException. As the exception name indicates, you are trying to perform network operations (download via internet) on the main thread. If this were allowed, you would end up leaving your app frozen for some time while the operation is running, which would result in a bad user experience and a non-responsive application. For this reason, it is not allowed to perform this type of operation on the main thread.

Therefore, the solution is to invoke the method getHttpGet in a thread that is not the main thread of the application. To do this, the easiest and obvious way would be use the class AsyncTask.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.