Android Studio中的IP查找问题无法解析Java主机地址
我在Java中的ip查找程序有问题,我导入了Inet地址,实现了异步任务,并在清单上使用了权限,但仍然显示了相同的问题。错误:无法解析主机“[Ljava.lang.String;@f1679a5”:没有与主机名相关的地址。我在互联网上搜索了所有地址,其中一些人说这是模拟器的问题,但我可以在我的设备上访问互联网。我已经完成了他们建议的所有操作,但仍然没有解决方案 以下是代码部分:Android Studio中的IP查找问题无法解析Java主机地址,java,android,performance,android-studio,Java,Android,Performance,Android Studio,我在Java中的ip查找程序有问题,我导入了Inet地址,实现了异步任务,并在清单上使用了权限,但仍然显示了相同的问题。错误:无法解析主机“[Ljava.lang.String;@f1679a5”:没有与主机名相关的地址。我在互联网上搜索了所有地址,其中一些人说这是模拟器的问题,但我可以在我的设备上访问互联网。我已经完成了他们建议的所有操作,但仍然没有解决方案 以下是代码部分: EditText hostnumber; TextView output; Button submit; Im
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;
}
}
}
我已经为这个问题驾驶了两个星期了,仍然没有解决方案。我重新启动了设备,与同事交谈,听了所有的互联网论坛和文档,但仍然没有任何线索。我还尝试了一个真实设备和5个虚拟设备以及相同的问题。请帮助!您似乎使用了变量名“hostnumber”引用一些不同的东西。这会造成混乱。您可以通过选择反映变量使用方式及其代表内容的名称来避免这种情况 在顶层,“hostnumber”是一个EditText GUI组件。您希望获取它包含的文本,而不是将组件本身转换为字符串。使用:
task.execute(hostnumber.getText().toString());
result = new connection.Result.Success(InetAddress.getByName(hostnumber[0]));
在AsyncTask.doInBackground方法中,hostnumber
是一个字符串数组。您需要使用数组中的第一个元素,因此写入hostnumber[0]
,而不是将数组本身转换为字符串。使用:
task.execute(hostnumber.getText().toString());
result = new connection.Result.Success(InetAddress.getByName(hostnumber[0]));