Android 应用程序无法从 Web 获取数据

Android App can't get Data from Web

为什么我收到此错误并且我的应用程序停止。我尝试更改所有域,并且所有域都可用。手机 Phone 有互联网连接,在清单中我没有忘记互联网前提。我不知道我忘记了什么。我认为这是因为我点击了按钮,我认为 isNetworkAvailable() 布尔值不支持 View v。

第 34 行:socket.connect(新 InetSocketAddress("example.com", 80), 2000);

第 97 行:if(isNetworkAvailable()) {

public void setnewuser(View v){
      EditText benutzername = (EditText) findViewById(R.id.benutzername);
      TextView error = (TextView) findViewById(R.id.error);


    if (benutzername.getText().toString().trim() != "") {

            String newusid = null;
            URL url = null;
            if(isNetworkAvailable()) {
                try {
                    url = new URL(gamehost + "/newuser.php");
                    String readLine = null;
                    String sResult = null;
                    BufferedReader buffReader = new BufferedReader(new InputStreamReader(url.openStream()));
                    while ((readLine = buffReader.readLine()) != null) {
                        if (sResult == null) {
                            sResult = readLine;
                        } else {
                            sResult = sResult + readLine;
                        }
                    }
                    newusid = sResult;
                } catch (MalformedURLException me) {
                    me.printStackTrace();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            String userid = newusid;
            if(userid != "") {
                editor.putString("username", benutzername.getText().toString().trim());
                editor.commit();
                editor.putString("userid", userid);
                editor.commit();
                startActivity(new Intent(Setname.this, Game.class));
                finish();
            }else{
                error.setText(R.string.needinternetforregist);
            }



    } else {
        error.setText(R.string.etwaseingeben);
    }

isNetworkAvailabe() 布尔值:

public boolean isNetworkAvailable() {
    try (Socket socket = new Socket()) {
        socket.connect(new InetSocketAddress("example.com", 80), 2000);
        return true;
    } catch (IOException e) {
        // Either we have a timeout or unreachable host or failed DNS lookup
        System.out.println(e);
        return false;
    }
}

Logcat:

                                                                              Process: de.gamer.myapp, PID: 13895
                                                                                 java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                                     at android.view.View.performClick(View.java:6213)
                                                                                     at android.widget.TextView.performClick(TextView.java:11074)
                                                                                     at android.view.View$PerformClick.run(View.java:23645)
                                                                                     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:6692)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                                  Caused by: java.lang.reflect.InvocationTargetException
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                     at android.view.View.performClick(View.java:6213) 
                                                                                     at android.widget.TextView.performClick(TextView.java:11074) 
                                                                                     at android.view.View$PerformClick.run(View.java:23645) 
                                                                                     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:6692) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
                                                                                  Caused by: android.os.NetworkOnMainThreadException
                                                                                     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
                                                                                     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
                                                                                     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
                                                                                     at java.net.InetAddress.getByName(InetAddress.java:708)
                                                                                     at java.net.InetSocketAddress.<init>(InetSocketAddress.java:232)
                                                                                     at de.gamer.Setname.isNetworkAvailable(Setname.java:34)
                                                                                     at de.gamer.Setname.setnewuser(Setname.java:97)

Caused by: android.os.NetworkOnMainThreadException

这意味着您正在尝试在主线程中连接到互联网。

你必须使用 AsyncTask