Skip to content Skip to sidebar Skip to footer

Android Httpurlconnection Url Doesn't Work On Emulator

I am trying to get json object as string from this url http://digitalcollections.tcd.ie/home/getMeta.php?pid=MS4418_021. It doesn't work I get an error after downloadUrl function.

Solution 1:

The problem was my antivirus/firewall on my computer. It was blocking my connection and that's why it was working on a external phone and not emulator. I disabled my antivirus/firewall and it worked. There is a list of network limitations here http://developer.android.com/tools/devices/emulator.html#networkinglimitations

Solution 2:

I just tried that URL on my device and didn't get any errors. Here is the code I used.

An Interface to get back onto the UI Thread

publicinterfaceAsyncResponse<T> {
    voidonResponse(T response);
}

A generic AsyncTask that returns a String - Feel free to modify this to parse your JSON and return a List.

publicclassWebDownloadTaskextendsAsyncTask<String, Void, String> {

    privateAsyncResponse<String> callback;

    publicvoidsetCallback(AsyncResponse<String> callback) {
        this.callback = callback;
    }

    @OverrideprotectedStringdoInBackground(String... params) {
        String url = params[0];
        returnreadFromUrl(url);
    }

    @OverrideprotectedvoidonPostExecute(String s) {
        super.onPostExecute(s);
        if (callback != null) {
            callback.onResponse(s);
        } else {
            Log.w(WebDownloadTask.class.getSimpleName(), "The response was ignored");
        }
    }

    privateStringstreamToString(InputStream is) throws IOException {

        StringBuilder sb = newStringBuilder();
        BufferedReader rd = newBufferedReader(newInputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }

    privateStringreadFromUrl(String myWebpage) {

        String response = null;
        HttpURLConnection urlConnection = null;

        try {
            URL url = newURL(myWebpage);

            urlConnection = (HttpURLConnection) url.openConnection();

            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {
                response = streamToString(inputStream);
                inputStream.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }

        return response;
    }
}

Section of my Activity to call the AsyncTask.

String url = "http://digitalcollections.tcd.ie/home/getMeta.php?pid=MS4418_021";

WebDownloadTask task = newWebDownloadTask();
task.setCallback(newAsyncResponse<String>() {
    @OverridepublicvoidonResponse(String response) {
        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
    }
});
task.execute(url);

Solution 3:

Make sure to use https instead of http to avoid these kind of errors on your Android Emulators.

privatestaticfinalStringBASE_URL="https://content.guardianapis.com/search?";

Post a Comment for "Android Httpurlconnection Url Doesn't Work On Emulator"