gpt4 book ai didi

java - 从 NPM json-server 获取数据抛出 NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-12-01 11:10:20 29 4
gpt4 key购买 nike

我正在编写一个小型 Android 应用程序,它应该从 npm json 服务器获取数据。它抛出异常。我确实使用 AsyncTask 扩展了连接类并执行了“doInBackground”中的代码,但它仍然抛出异常。请帮助我。

代码如下:

import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Network extends AsyncTask <String, Void, String> {
public String urlString = ("http://localhost:3000");

@Override
protected String doInBackground(String... params) {
StringBuffer chaine = new StringBuffer("");
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agnet", "");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();

InputStream inputStream = connection.getInputStream();

BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
chaine.append(line);
}

} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();

}
return chaine.toString();
}
}

logcat 告诉我们:

android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) at java.net.InetAddress.lookupHostByName(InetAddress.java:418) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) at java.net.InetAddress.getAllByName(InetAddress.java:215) at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29) at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232) at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:361) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:289) at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373) at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106) at smoca.ch.kreagen.Network.doInBackground(Network.java:25) at smoca.ch.kreagen.BackendMockup.getData(BackendMockup.java:11) at smoca.ch.kreagen.FillRealm.initRealm(FillRealm.java:43) at smoca.ch.kreagen.Fragments.HomeFragment.onCreateView(HomeFragment.java:42) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

我知道问题出在“connection.connect();”时被调用是因为它应该在主线程中。但不应该。对吗?

问候约翰

最佳答案

    public class Network extends AsyncTask <String, Void, String> {
public String urlString = ("http://localhost:3000");
HttpURLConnection connection;
URL url;
StringBuffer chaine;

@Override
protected String doInBackground(String... params) {

Thread thread = new Thread(new Runnable() {
@Override
public void run() {
chaine = new StringBuffer("");
try {
url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agnet", "");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();

InputStream inputStream = connection.getInputStream();

BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
chaine.append(line);
}

} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();

}
}
});
thread.start();

return chaine.toString();
}
}

关于java - 从 NPM json-server 获取数据抛出 NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32452434/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com