Jafar Albadarneh
Jafar Albadarneh

Reputation: 415

android can't establish URL connection

i've an application that was act properly when i run it with android 2.3.6(Ginger Bread), but when i run it on 4.2 it did'nt work with the URL connection, it can't establish the connection here is the snip of the code that performs the URL connection :

try {
                Add.setEnabled(true);
                movieContent.setText("");
                String TITLE =searchET.getText().toString();
                TITLE = TITLE.replaceAll(" ", "%20");
                Log.d("title after parsing:", TITLE);
                URL url = new URL("http://www.omdbapi.com/?i=&t="
                        + TITLE);
                String URL2="http://www.omdbapi.com/?i=&t=saw";
                Log.d("URL content", url.toString());
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();
                Log.d("URL content", "register URL");
                urlConnection.connect();
                Log.d("URL connection", "establish connection");

the log cat shows that the flow reach "register URL" log print it then raise a warning in green labeled as QCNEA:

03-21 23:53:56.596: D/title after parsing:(16623): saw 03-21 23:53:56.596: D/URL content(16623): http://www.omdbapi.com/?i=&t=saw 03-21 23:53:56.596: D/URL content(16623): register URL 03-21 23:53:56.596: I/QCNEA(16623): |NIMS| getaddrinfo: hostname www.omdbapi.com servname NULL numeric 4 appname 03-21 23:53:56.616: I/MediaPlayer(16623): Don't send intent. msg.arg1 = 0, msg.arg2 = 0 03-21 23:53:57.207: V/MediaPlayer(16623): message received msg=2, ext1=0, ext2=0 03-21 23:53:57.207: V/MediaPlayer(16623): playback complete 03-21 23:53:57.207: V/MediaPlayer(16623): callback application 03-21 23:53:57.207: V/MediaPlayer(16623): back from callback

knowing that all permission regarding connection are added.

the weird thing that there is no error , just can't establish the connection.

hope you guys help me with this thanks in advance

Upvotes: 1

Views: 1625

Answers (3)

Morty
Morty

Reputation: 774

First, append following into AndroidManifest.xml

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

Second, try this

int timeout = 15 * 1000; // timeout in 15 seconds 

HttpURLConnection urlConnection = (HttpURLConnection) url
                    .openConnection();

urlConnection.setConnectTimeout(timeout);
urlConnection.setReadTimeout(timeout);
urlConnection.connect();

// ...

Upvotes: 0

Laura Pulido
Laura Pulido

Reputation: 161

The difference between gingerbread and higher versions is the strict policy about networking on main thread. When you change strict mode to permit all you allow tour activities to do that networking on main thread... But you shouldnt do that... You should go by the rules and run an async task Just like the one suggested by @JustSoAmazing.

Upvotes: 0

JustSoAmazing
JustSoAmazing

Reputation: 747

I suspect you might be running into issues with doing networking code on the main thread, though I'm not sure why you're not getting an error or app crash.

Put your code in an Async task and see if it works.

AsyncTask<Void, Void, Void> asyncLoad = new AsyncTask<Void, Void, Void>()
    {
        @Override
        protected Void doInBackground(Void... params)
        {
            URL url = new URL("http://www.omdbapi.com/?i=&t="
                    + TITLE);
            String URL2="http://www.omdbapi.com/?i=&t=saw";
            Log.d("URL content", url.toString());
            HttpURLConnection urlConnection = (HttpURLConnection) url
                    .openConnection();
            Log.d("URL content", "register URL");
            urlConnection.connect();
            Log.d("URL connection", "establish connection");

            return null;
        }

        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
        }
    };

    asyncLoad.execute();

Upvotes: 1

Related Questions