作者akito117 (波吉)
看板AndroidDev
標題[問題] 請問從API抓回來的資料能再抓一次資料嗎
時間Fri May 29 17:54:48 2020
@Override
protected void onPostExecute(String message) {
super.onPostExecute(message);
items = new ArrayList<>();
JSONArray rows;
try {
json_read = new JSONObject(message);
gps = json_read.getJSONArray("rows");
for (int i = 0; i < gps.length(); i++) {
JSONObject row = gps.getJSONObject(i);
Map<String, Object> item = new HashMap<>();
String Url = "
http://xxxx/getAddress.ashx?h=" +
row.getString("lat") + "," + row.getString("lng");
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(Url);
HttpResponse response = httpClient.execute(get);
HttpEntity resEntity = response.getEntity();
//Address = EntityUtils.toString(resEntity);
item.put("address", EntityUtils.toString(resEntity));
} catch (Exception e) {
item.put("address", e.toString());
}
items.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
會報android.os.NetworkOnMainThreadException
請問大概要怎麼解
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.61.125.247 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/AndroidDev/M.1590746090.A.FAB.html
1F:推 XperiaZ6C: 你要開一個thread處理吧,不然你app就anr了 05/29 18:02
2F:→ rin841005: 耗時的工作要在另外的執行緒執行, onPostExecute已經在 05/29 18:10
3F:→ rin841005: 主執行緒了, 應該要寫在doInBackground 05/29 18:10
4F:→ kobe8112: 嗯....這個例外的英文不是超級白話嗎= =? 05/29 18:29
5F:→ akito117: 感謝(椅腦s・`) 05/31 13:48
6F:→ akito117: 感謝 05/31 13:48