gpt4 book ai didi

java - OnPostExecute 被跳过

转载 作者:行者123 更新时间:2023-12-02 11:08:15 25 4
gpt4 key购买 nike

当我使用 new GiveMeJson2.execute(ref_id).get() 调用异步类时,

我的应用程序进入 doInBackground,创建一个 JSON 字符串并返回它。

此时,我认为应用程序必须进入 onPostExecute 并解析该 JSON。

public class LiveArrivalActivity extends AppCompatActivity {
ListViewLiveArrival adapter;
ListView listView;
public static ArrayList<LiveArrivalHelper> list = new ArrayList<LiveArrivalHelper>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_arrival);

listView = findViewById(R.id.listView);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

Bundle b = getIntent().getExtras();
Station station = (Station) b.get("Station");
String ref_id = station.getRef_id();
try {
new GiveMeJson2().execute(ref_id).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.i("OUT", list.toString());

adapter = new ListViewLiveArrival(this, list);
listView.setAdapter(adapter);
} }

class GiveMeJson2 extends AsyncTask<String, Void, String> {
String jsonText = "";
@Override
protected String doInBackground(String... stations) {

try {
URL url = new URL("https://www.trola.si/" + stations[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Accept", "application/json");
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
jsonText = readAll(bufferedReader);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonText;
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONArray jsonArray = new JSONObject(s).getJSONArray("stations");
JSONArray buses = jsonArray.getJSONObject(0).getJSONArray("buses");
for (int i = 0; i < buses.length(); i++) {
JSONObject jsonObject = buses.getJSONObject(i);
String direction = jsonObject.getString("direction");
String number = jsonObject.getString("number");
JSONArray arrivalsArray = jsonObject.getJSONArray("arrivals");


if (arrivalsArray.length() > 0){
String arrivals = "";
for (int j = 0; j < arrivalsArray.length(); j++) {
arrivals += arrivalsArray.get(j) + " min ";
}
Log.i("OUT", number+direction+arrivals.toString());
LiveArrivalHelper liveArrivalHelper = new LiveArrivalHelper(direction,number,arrivals);
LiveArrivalActivity.list.add(liveArrivalHelper);
}
}

} catch (JSONException e) {
e.printStackTrace();
}
}

private String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
} }

关于为什么这样做有什么建议吗?

最佳答案

当您在 AsyncTask 上调用 .get() 时,它会强制将其称为同步。在本例中是在主线程上。

这是不允许的。
所以AsyncTask进入doInBackground,那么Android系统立即抛出错误。

如果您查看 LogCat,错误将打印在那里。

您需要从 AsyncTask 中删除 .get() 并实际异步处理它。

关于java - OnPostExecute 被跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50773469/

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