java - 使用InetAddress时出现Java/Android错误

62 4

我试图在android即时通讯应用程序中实现ping,但是当尝试执行时,调试控制台会显示以下错误:

控制台中显示的错误信息


I/System.out: Sending Ping Request to 127.0.0.1


I/Error starts here: -----------------------------------


W/System.err: android.os.NetworkOnMainThreadException


W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)


 at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:364)


 at libcore.io.ForwardingOs.sendto(ForwardingOs.java:194)


 at libcore.io.IoBridge.sendto(IoBridge.java:556)


 at java.net.Inet6AddressImpl.icmpEcho(Inet6AddressImpl.java:267)


W/System.err: at java.net.Inet6AddressImpl.isReachable(Inet6AddressImpl.java:208)


 at java.net.InetAddress.isReachable(InetAddress.java:525)


 at java.net.InetAddress.isReachable(InetAddress.java:480)


 at com.h4lfcr3st.androidswissarmy.PingUtility.BtnMakePing(PingUtility.java:36)


 at java.lang.reflect.Method.invoke(Native Method)


 at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)


W/System.err: at android.view.View.performClick(View.java:7125)


 at android.view.View.performClickInternal(View.java:7102)


 at android.view.View.access$3500(View.java:801)


 at android.view.View$PerformClick.run(View.java:27336)


 at android.os.Handler.handleCallback(Handler.java:883)


 at android.os.Handler.dispatchMessage(Handler.java:100)


 at android.os.Looper.loop(Looper.java:214)


W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7356)


 at java.lang.reflect.Method.invoke(Native Method)


 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)


 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)



我已经尝试过intellij中的代码,并且工作的很好,我相信这是android上网权限问题,已声明:


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



在清单中,Java代码如下:


import androidx.appcompat.app.AppCompatActivity;



import android.os.Bundle;


import android.util.Log;


import android.view.View;


import android.widget.EditText;


import android.widget.Toast;



import java.io.*;


import java.net.*;



public class PingUtility extends AppCompatActivity {



 private EditText HostToPing, pingResults;



 @Override


 protected void onCreate(Bundle savedInstanceState) {


 super.onCreate(savedInstanceState);


 setContentView(R.layout.activity_ping_utility);



 HostToPing = findViewById(R.id.ETIpHost);


 pingResults = findViewById(R.id.ETLogIp);



 }



 public void BtnMakePing(View view){ //todo corregir


 String ipToPing = HostToPing.getText().toString();


 try{


 Log.i("IpToPing","----------------"+ ipToPing);


 InetAddress geek = InetAddress.getByName(ipToPing);


 Toast.makeText(this,"Sending Ping request to" + ipToPing, Toast.LENGTH_SHORT).show();


 System.out.println("Sending Ping Request to" + ipToPing);


 boolean recheable = geek.isReachable(10000);


 pingResults.setText("The host its recheable =" + recheable);


 }catch (Exception e){


 Log.i("Error starts here" ,"-----------------------------------");


 e.printStackTrace();


 Toast.makeText(this,"Error", Toast.LENGTH_SHORT).show();


 }



 }


}



JAVA代码

时间: 原作者:

125 2

android中的网络操作无法在主线程(活动线程)中运行,你应该创建一个子线程来调用网络操作async并通过回调接收结果。

原作者:
74 0

你不应该在MainThread中做你的网络,只需将BtnMakePing方法放到另一个线程中。

原作者:
...