简体   繁体   中英

Problem With IP Lookup in Android Studio Unable To resolve Host Address Java

I'm having a problem with my ip lookup program in Java I've imported Inet Adress and implementd the async task and used the permission on the manifest but still shows the same problem. The Error: Unable to resolve host "[Ljava.lang.String;@f1679a5": No address associated with hostname I've been seaching all over the internet and some of them say that It's the emulator problem BUT I can access internet on my device I've done everything they've recommended but still no solutions.

Here is the code Part:

    EditText hostnumber;
TextView output;
Button submit;
ImageView back;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lookup);
    back = findViewById(R.id.backlearn);

hostnumber = findViewById(R.id.hostnumber);
output = findViewById(R.id.outputDecimal);
submit = findViewById(R.id.submit);

back.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Lookup.this, MainActivity.class);
        startActivity(intent);
    }
});
submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        @SuppressLint("StaticFieldLeak") AsyncTask<String, Void, connection.Result<InetAddress>> task = new AsyncTask<String, Void, connection.Result<InetAddress>>() {
            // NOTE: this method runs in a background thread, you cannot access the View from here
            @Override
            protected connection.Result<InetAddress> doInBackground(String... hostnumber) {
                connection.Result<InetAddress> result;
                try {
                    result = new connection.Result.Success(InetAddress.getByName(String.valueOf(hostnumber)));
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                    result = new connection.Result.Failure(e);
                }catch (Exception e) {
                    result = new connection.Result.Failure(e);
                }

                return result;
            }


            @Override
            protected void onPostExecute(connection.Result<InetAddress> result) {
                if (result instanceof connection.Result.Success) {
                    output.setText((CharSequence) ((connection.Result.Success<InetAddress>) result).data);
                } else if (result instanceof connection.Result.Failure) {
                   Throwable error = ((connection.Result.Failure<InetAddress>) result).error;
                    Toast.makeText(Lookup.this,"Please Check The Address!: " +error, Toast.LENGTH_SHORT).show();

                }
            }
        };

        task.execute(hostnumber.toString());
    };
})
;}

}
class connection extends Activity {
    static class Result<T> {
        static class Success<T> extends Result<T> {
            public T data;
            public Success(T data) {
                this.data = data;
            }
        }
        static class Failure<T> extends Result<T> {
            public Throwable error;
            public Failure(Throwable error) {
                this.error = error;
            }
        }
    }

And here is the error part:

W/System.err: java.net.UnknownHostException: Unable to resolve host "[androidx.appcompat.widget.AppCompatEditText{f465e26 VFED..CL. .F...... 158,160-923,275 #7f0800b1 app:id/hostnumber}]": No address associated with hostname at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:156) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103) at java.net.InetAddress.getByName(InetAddress.java:1106) at com.modex.smartcalculator.Lookup$2$1.doInBackground(Lookup.java:50) at com.modex.smartcalculator.Lookup$2$1.doInBackground(Lookup.java:44) at android.os.AsyncTask$3.call(AsyncTask.java:378) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:919) Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname) at libcore.io.Linux.android_getaddrinfo(Native Method) at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74) at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:200) W/System.err:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135) ... 10 more

I've been driving with this problem for two weeks now and still no solution I've rebooted the device, talked with colleagues listened to all internet forums and documentations but still no clue with this I also tried with a real device and 5 virtual devices and the same problem. Please Help!

You seem to use the variable name "hostnumber" to refer to a few different things. This will create confusion. You can avoid this by picking names that reflect how the variable is used and what it stands for.

On the top level, "hostnumber" is an EditText GUI component. You want to get the text it contains , instead of transforming the component itself into a string. Use:

task.execute(hostnumber.getText().toString());

In the AsyncTask.doInBackground method, hostnumber is an array of Strings. You'll want to use the first element from the array, so write hostnumber[0] instead of transforming the array itself into a string. Use:

result = new connection.Result.Success(InetAddress.getByName(hostnumber[0]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM