0

im triying to implement a ping option in my android app, but when y tried the following error is displayed in the debug console:

Error info displayed in console

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)

I already try the code in intellij and it works just fine, i belive that the problem its with android permisions for internet access. already declared:

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

in the manifest. the java code its the following:

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 code

sorry for my speeling, im not native english speaker

NormanP
  • 1
  • 2

2 Answers2

0

Network action in android couldn't run in main thread (Activity thread). You should create a sub-thread to call the network action async and receive the result via callback.

Gavin Luo
  • 1
  • 1
0

You should not do your network in the MainThread, just put the BtnMakePing method in an other thread.

L. Swifter
  • 3,179
  • 28
  • 52