在Android Studio中进行IP查找时出现问题,无法解析主机地址Java。

huangapple go评论58阅读模式
英文:

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

问题

我在Java中的IP查找程序中遇到了问题我已经导入了InetAddress并实现了异步任务并在清单文件中使用了权限但问题仍然存在错误信息无法解析主机[Ljava.lang.String;@f1679a5”:主机名没有关联的地址我已经在互联网上搜索了很多其中一些人说这是模拟器的问题但是我可以在我的设备上访问互联网我已经尝试了他们推荐的所有方法但仍然没有解决方案

以下是代码部分

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>>() {
                @Override
                protected connection.Result<InetAddress> doInBackground(String... hostnumber) {
                    connection.Result<InetAddress> result;
                    try {
                        result = new connection.Result.Success(InetAddress.getByName(String.valueOf(hostnumber[0])));
                    } 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(((connection.Result.Success<InetAddress>) result).data.toString());
                    } else if (result instanceof connection.Result.Failure) {
                        Throwable error = ((connection.Result.Failure<InetAddress>) result).error;
                        Toast.makeText(Lookup.this, "请检查地址!错误:" + error, Toast.LENGTH_SHORT).show();
                    }
                }
            };

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

class connection {
    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;
            }
        }
    }
}

错误部分:

W/System.err: java.net.UnknownHostException: 无法解析主机[androidx.appcompat.widget.AppCompatEditText{f465e26 VFED..CL. .F...... 158,160-923,275 #7f0800b1 app:id/hostnumber}]”:主机名没有关联的地址
...

我已经为这个问题苦苦挣扎了两周,但仍然没有解决方案。我已经重新启动了设备,与同事交流,并阅读了所有的互联网论坛和文档,但对于这个问题仍然一筹莫展。我还尝试了真实设备和5个虚拟设备,但问题仍然存在。请帮忙解决!

英文:

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(&quot;StaticFieldLeak&quot;) AsyncTask&lt;String, Void, connection.Result&lt;InetAddress&gt;&gt; task = new AsyncTask&lt;String, Void, connection.Result&lt;InetAddress&gt;&gt;() {
// NOTE: this method runs in a background thread, you cannot access the View from here
@Override
protected connection.Result&lt;InetAddress&gt; doInBackground(String... hostnumber) {
connection.Result&lt;InetAddress&gt; 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&lt;InetAddress&gt; result) {
if (result instanceof connection.Result.Success) {
output.setText((CharSequence) ((connection.Result.Success&lt;InetAddress&gt;) result).data);
} else if (result instanceof connection.Result.Failure) {
Throwable error = ((connection.Result.Failure&lt;InetAddress&gt;) result).error;
Toast.makeText(Lookup.this,&quot;Please Check The Address!: &quot; +error, Toast.LENGTH_SHORT).show();
}
}
};
task.execute(hostnumber.toString());
};
})
;}
}
class connection extends Activity {
static class Result&lt;T&gt; {
static class Success&lt;T&gt; extends Result&lt;T&gt; {
public T data;
public Success(T data) {
this.data = data;
}
}
static class Failure&lt;T&gt; extends Result&lt;T&gt; {
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 &quot;[androidx.appcompat.widget.AppCompatEditText{f465e26 VFED..CL. .F...... 158,160-923,275 #7f0800b1 app:id/hostnumber}]&quot;: 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!

答案1

得分: 1

你似乎在几个不同的地方使用了变量名“hostnumber”。这将会引起混淆。您可以通过选择反映变量用途和含义的名称来避免这种情况。

在顶层,“hostnumber”是一个EditText GUI组件。您想要获取其包含的文本,而不是将组件本身转换为字符串。使用:

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

在AsyncTask.doInBackground方法中,hostnumber是一个字符串数组。您需要使用数组中的第一个元素,所以请使用hostnumber[0],而不是将数组本身转换为字符串。使用:

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

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]));

huangapple
  • 本文由 发表于 2020年8月21日 04:33:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63512762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定