与Android App中的服务器通信不起作用

时间:2018-08-01 07:54:52

标签: java android

我对获取页面内容的方法有疑问。我已经添加了所有重要的细节。

我在我的android清单中授予了互联网许可:

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

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)

这是运行此方法时出现的完整错误:

@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"));
        }
    });
}

在此代码部分中,我使用正确的url调用该方法:

...X
.X..
..X.
...X

1 个答案:

答案 0 :(得分:0)

错误日志不完整,缺少顶行(带有异常名称和消息)。
首先检查(很明显)是否可以使用浏览器访问此URL。
确保您的应用具有Internet(或文件读取)权限。 (将<uses-permission android:name="android.permission.INTERNET" />添加到您的应用清单中)

此外,两个参数字符串名称都与类名称相同;虽然我不认为这是问题的原因,但它使代码混乱,应避免使用。尝试将字符串名称更改为targetUrl

编辑:读取错误日志很明显。似乎Android不允许在主线程上进行网络通信(因为阻止它也会阻止渲染)。我建议在这种情况下使用AsyncTask。有关更多信息,请查看this question