1

I have a problem with a method to get the content of a page. I have added all important details.

I have give the internet permission in my android manifest:

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:usesCleartextTraffic="true"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Start">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Home"
        android:label="@string/title_activity_home"
        android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

Here is the complete error that I become when I run this method:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: professionalbans.tutorialwork.de.professionalbans, PID: 24732
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
    at java.net.InetAddress.getAllByName(InetAddress.java:1154)
    at com.android.okhttp.Dns$1.lookup(Dns.java:39)
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
    at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
    at java.net.URL.openStream(URL.java:1072)
    at professionalbans.tutorialwork.de.professionalbans.WebManager.ReadURL(WebManager.java:17)
    at professionalbans.tutorialwork.de.professionalbans.RecyclerViewAdapter$1.onClick(RecyclerViewAdapter.java:42)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

And in this code part I call the method with a correct url:

@Override
public void onBindViewHolder(@NonNull ViewHolderClass holder, int position) {
    holder.itemText.setText(Home.spieler.get(position));
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Item Click
            System.out.print("Answer: "+WebManager.ReadURL("http://tutorialwork.bplaced.net/dev/test.php"));
        }
    });
}
Tutorialwork
  • 63
  • 2
  • 9

1 Answers1

1

The error log is incomplete, the top line(with exception name and message) is missing.
First check (quite obviously) if you can access this URL with your browser.
Make sure that your app has Internet(or file reading) permission. (Add <uses-permission android:name="android.permission.INTERNET" /> to your app manifest)

Also, both the argument string name is the same as class name; while I don't think this is the cause of the problem, it is confusing code and should be avoided. Try changing the String name to something like targetUrl

Edit: Reading the error log it becomes obvious. Seems like Android doesn't allow Network communication on main thread(since blocking it also blocks the rendering). I suggest using AsyncTask in this situation. For more info view this question

Mk Km
  • 94
  • 6