Android App无法从Web获取数据[重复]

Posted

技术标签:

【中文标题】Android App无法从Web获取数据[重复]【英文标题】:Android App can't get Data from Web [duplicate] 【发布时间】:2017-11-21 19:52:50 【问题描述】:

为什么我收到此错误并且我的应用程序停止。我尝试更改所有域并且所有域都可用。手机有 Internet 连接,在 Manifest 中我不会忘记 Internet 权限。我不知道我忘记了什么。我认为这是因为我点击了 Button 并且我认为 isNetworkAvailable() boolean 不支持 View v。

Line:34 : socket.connect(new 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)

【问题讨论】:

【参考方案1】:

Caused by: android.os.NetworkOnMainThreadException

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

你必须使用 AsyncTask

【讨论】:

您有什么想法吗?我从 java 开始,不能完美地用 Java 编程。 java和android不一样 是的,我知道,但很相似

以上是关于Android App无法从Web获取数据[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Flutter Web和Firebase身份验证TypeError:无法读取未定义的属性'app'

获取用于从 Android 设备连接到 Firebase 的 IP 地址 [重复]

无法在 Web 服务中获取参数 [重复]

从 Fragment (support.v4) 中获取 FragmentManager [重复]

Android从相机捕获图像返回空数据[重复]

无法从 Flutter Web 上的 localhost API(node js express)获取 http 请求的响应 [重复]