我一直在创建一个关于检查x网站ip的应用程序,我已经导入了:java.net.InetAddress和它将在intelij上工作,但在android studio上,try/catch块捕获了一些错误:android.os.NetworkOnMainThreadException。所以这是代码部分:
AsyncTask.execute(new Runnable() {
@Override
public void run() {
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InetAddress address = null;
try {
address = InetAddress.getByName(String.valueOf(hostnumber));
} catch (UnknownHostException e) {
e.printStackTrace();
}catch (Exception e) {
Toast.makeText(Lookup.this,"Problem"+e, Toast.LENGTH_SHORT).show();
}
output.setText((CharSequence) address);
}
});
}
});
I've added the internet permission on manifest: <uses-permission android:name="android.permission.INTERNET" />
The try/catch block caught: 'android.os.NetworkOnMainThreadException'
我从堆栈中看到了一些答案,例如:如何修复';android.os.NetworkOnMainThreadException',https://www.tutorialspoint.com/how-to-fix-android-os-networkonmainthreadexception
但它仍然不会显示网站的IP,并再次显示错误。如果你有解决这个问题的办法,请联系我。提前感谢!
更新:我试图放入一个布尔语句,但它更糟,抛出了更多的错误。这些块的语法可能是错误的格式吗?我应该在哪里修复代码
您遇到了问题,因为在Android中,您只能访问MainThread中的View,而只能访问WorkerThread的Network。
另外,创建AsyncTask的方式也不正确。
我几年前就已经完全改用Kotlin了,我已经有三年没有写Java了,但这应该是可行的:
public class Testss 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;
}
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
submit.setOnClickListener(new View.OnClickListener() {
AsyncTask<String, Void, Result<InetAddress>> task = new AsyncTask<String, Void, Result<InetAddress>>() {
// NOTE: this method runs in a background thread, you cannot access the View from here
@Override
protected Result<InetAddress> doInBackground(String... hostnumber) {
Result<InetAddress> result;
try {
result = new Result.Success(InetAddress.getByName(String.valueOf(hostnumber)));
} catch (UnknownHostException e) {
e.printStackTrace();
result = new Result.Failure(e);
}catch (Exception e) {
result = new Result.Failure(e);
}
return result;
}
// NOTE: this method runs in the mainthread
@Override
protected void onPostExecute(Result<InetAddress> result) {
if (result instanceof Result.Success) {
output.setText((CharSequence) ((Result.Success<InetAddress>) result).data);
} else if (result instanceof Result.Failure) {
Throwable error = ((Result.Failure<InetAddress>) result).error
Toast.makeText(Lookup.this,"Problem"+error, Toast.LENGTH_SHORT).show();
}
}
};
task.execute(hostnumber);
};
}
}
嗨,我按照Andre的建议做了这件事,它在某种程度上解决了问题和代码:
EditText hostnumber;
TextView output;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lookup);
hostnumber = findViewById(R.id.hostnumber);
output = findViewById(R.id.output);
submit = findViewById(R.id.submit);
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;
}
}
}
}
但我仍然面临这个类的问题
这是XML文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Lookup">
<EditText
android:id="@+id/hostnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="98dp"
android:ems="10"
android:inputType="textUri|textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="61dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/hostnumber" />
<TextView
android:id="@+id/output"
android:layout_width="100dp"
android:layout_height="47dp"
android:layout_marginStart="63dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="312dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.186"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/submit" />
</androidx.constraintlayout.widget.ConstraintLayout>
我已经尝试了很多关于修复互联网部分的解决方案,但仍然没有线索。如果你对此有任何想法,或者我错在哪里了。。谢谢1