作者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/cn.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