HttpResponse using android issue: execute always causes exception? HttpResponse using android issue: execute always causes exception? android android

HttpResponse using android issue: execute always causes exception?


Check the permissions of your manifest, make sure you have added this:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><uses-permission android:name="android.permission.INTERNET"/>


Figured it out. Apparently I turned my AVD off of "airplane mode".

For anybody having the same problem as me, you're probably using wireless. For some reason the android looks at your LAN card, so go to your Network Connections and right click on the LAN card-- DISABLE THAT THING! Problem solved.


I ran into this problem as well. In my case, I am using HttpURLConnection to load JSON. The issue was fixed when I enabled "follow redirects." Here is a code-snippet that works:

    HttpURLConnection connection = null;    BufferedReader reader = null;    String JSON_data = null;    try {        final int half_hour = 30 * 60;        URL fetch = new URL(requestUrl);        connection = (HttpURLConnection) fetch.openConnection();        // FIXED: request to follow redirects - this seems to solve networking issues on older devices < API 21        connection.setInstanceFollowRedirects(true);        connection.setUseCaches(true);        connection.setDefaultUseCaches(true);        connection.setRequestMethod("GET");        connection.addRequestProperty("Cache-Control", "max-age="+half_hour);        connection.addRequestProperty("X-Auth-Token", getString(R.string.api_key));        boolean redirect = false;        int status = connection.getResponseCode();        if (status != HttpURLConnection.HTTP_OK) {            if (status == HttpURLConnection.HTTP_MOVED_TEMP                    || status == HttpURLConnection.HTTP_MOVED_PERM                    || status == HttpURLConnection.HTTP_SEE_OTHER)                redirect = true;        }        Log.d(TAG, "===> HTTP STATUS CODE: " + status + ", redirect=" + redirect);        connection.connect();        // Read the input stream into a String        InputStream inputStream = connection.getInputStream();        if (inputStream == null) {            return;        }        reader = new BufferedReader(new InputStreamReader(inputStream));        StringBuilder buffer = new StringBuilder();        String line;        while ((line = reader.readLine()) != null) {            buffer.append(line);            buffer.append("\n");        }        if (buffer.length() == 0) {            return;        }        JSON_data = buffer.toString();        Log.d(TAG, "JSON_data=" + JSON_data);    } catch (Exception e) {        // possible UnknownHostException on older Android device?        Log.e(TAG, "HttpURLConnection Exception - e=" + e.getMessage());        e.printStackTrace();    } finally {        if (connection != null) {            connection.disconnect();        }        if (reader != null) {            try {                reader.close();            } catch (IOException e) {                Log.e(TAG, "Error closing stream - e=" + e.getMessage());            }        }    }