当我使用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();
} }
关于它为什么这样做的任何建议?
答案 0 :(得分:1)
当您在.get()
上呼叫AsyncTask
时,会强制将其称为同步。在这种情况下,在主线程上。
不允许这样做 所以AsyncTask进入doInBackground,然后Android系统立即抛出错误。
如果您查看LogCat,则会在那里打印错误。
您需要从AsyncTask中删除.get()并实际处理它为异步。
答案 1 :(得分:0)
使用.get()调用AsyncTask类时,例如新的GiveMeJson2.execute(ref_id).get(),而不是onPreExecute和onPostExecute。