0

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)
4
  • 2
    Please add complete Logcat to your question. Commented Jan 5, 2017 at 19:49
  • It is nothing special in it expect any log-output I put in before the getResponseCode. And then the "NULL" of the exceptions getMessage (). Commented Jan 5, 2017 at 19:54
  • 1
    I'm interested in Stack Trace. I hope you do not suppress it. Commented Jan 5, 2017 at 19:55
  • Added the stack trace. Commented Jan 5, 2017 at 20:03

2 Answers 2

4
 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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.