gpt4 book ai didi

java - 网络操作导致 android.os.NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-12-01 16:21:56 24 4
gpt4 key购买 nike

我试图编写一个应用程序,它从网站获取一些 JSON,将其转换为数组,然后将该数组的特定元素放入应用程序中表格的不同位置。我编写了获取 JSON 数据并将其转换为 Eclipse 中的 ArrayList 的代码。那里效果很好。我现在尝试在 Android Studio 中实现代码,但无法让它工作。我有以下代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);
TextView textview = (TextView) findViewById(R.id.textView8);
try{
textview.setText(MainActivity.get_data().toString());
}
catch (Exception e){
textview.setText(e.getClass().getCanonicalName());
}
}

public static ArrayList<String> get_data() throws Exception{
String[] ids = new String[] {(here are some ids)};
ArrayList<String> alldata = new ArrayList<>();
for(int r=0;r<ids.length;r++) {
String url = "https://ktrax.kisstech.ch/backend/tracking?db=aviation&sw_lat=40.97765930663377&sw_lon=-26.280096606906117&ne_lat=43.01854550507729&ne_lon=-23.407171802218617&ktrax_id=icao%3a" +ids[r]+"&format=json";
URL ktraxURL = new URL(url);
JSONArray test = Networkaccess.getJSONarr(ktraxURL);
ArrayList<String> listdata = MainActivity.converter(test);
ArrayList<String> elementlist = new ArrayList<>();
(some more code (irrelevant in this matter) which makes the array nice and neat)

//System.out.println(alldata);
return alldata;
}

public static ArrayList<String> converter(JSONArray test){ //creates an array with all the data splited

(Some code that converts JSON to ArrayList (also no problems here))
}
}

class Networkaccess{
public static JSONArray getJSONarr(URL ktraxURL) throws Exception{
HttpURLConnection con = (HttpURLConnection) ktraxURL.openConnection();
//Checking for reponse code of GET Method
con.setRequestMethod("GET");


!!!!!! int responseCode = con.getResponseCode(); //THE DEBUGGER JUMPS FROM HERE TO THE CATCH IN MAIN



System.out.println("Response Code : " +responseCode);
//Reading Data and
BufferedReader readin = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = readin.readLine()) != null) {
response.append(inputLine);
}
readin.close();
//System.out.println(response.toString());
String jsonresp = new String(response.toString());
//System.out.println(jsonresp);
JSONObject myResponse = new JSONObject(jsonresp);
JSONArray test = myResponse.getJSONArray("targets");
//System.out.println(test);
return test;
}
}

我创建了新的网络类作为错误,该错误显示在 TextView 字段中:android.os.NetworkOnMainThreadException。然而,这似乎并没有完成这项工作,并且仍然显示相同的错误。我不知道如何从这里继续。调试时,调试器始终运行到我用 !!! 标记的行!在代码中。然后跳转到主activity中的catch异常处。

期待您的答复!

最佳答案

网络内容往往会堵塞,因为响应可能需要一段时间才能传递。它的意思是,您需要将代码移至另一个线程,以便您的程序在等待响应时不会卡住。

这不会阻止与网络相关的速度下降,但会消除错误:

Thread networking = new Thread(){
void run() {
int responseCode = con.getResponseCode();
}
}
networking.start();
networking.join();

关于java - 网络操作导致 android.os.NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62247166/

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