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)