chris01
chris01

Reputation: 12431

Android Java: HttpURLConnection

I try to explore Android and do some samples.

Thats my code.

    try
    {
        URL url = new URL("http://google.com");
        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setReadTimeout (10 * 1000);

        int rc = uc.getResponseCode();
    }
    catch (Exception e)
    {

    }

It is working perfectly if I run under Linux.

But if I put it into Android I get a NULL Exception at getResponseCode.

Tried it in the emulator of Android Studio. Network-connections seem to be ok, Chrome in the emulator is working well.

I tried it in onCreate as well as in a Click-Listener of a Button. It is imported from java.net.

The Manifest does have the

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

included.

Any ideas?? Thanks!

EDIT: Thats the stackktrace after called getResponseCode.

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.ch.my1stapplication, PID: 3435
              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                  at com.example.ch.my1stapplication.MainActivity$4.onClick(MainActivity.java:173)
                  at android.view.View.performClick(View.java:5637)
                  at android.view.View$PerformClick.run(View.java:22429)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Upvotes: 0

Views: 1352

Answers (2)

Anurag Aggarwal
Anurag Aggarwal

Reputation: 247

 private class Task extends AsyncTask<Void , Void , Void>{
        @Override
        protected Void doInBackground(Void... voids) {
            try
            {
                URL url = new URL("http://google.com");
                HttpURLConnection uc = (HttpURLConnection) url.openConnection();
                uc.setReadTimeout (10 * 1000);

                int rc = uc.getResponseCode();
                Log.d("rc" , rc+"");
            }
            catch (Exception e)
            {

            }
            return null;
        }
    }

 new Task().execute(); 

You need to create a thread to handle network calls . As you are running the network calls on the main thread so it is not executing the program successfully.

Upvotes: 4

Andrey  Kopeyko
Andrey Kopeyko

Reputation: 1566

It seems you are trying to do networking inside main UI thread - this is forbidden from 11+. You need to use AsyncTask or AsyncLoader to perform network calls.

Upvotes: 2

Related Questions