[問題] 請問從API抓回來的資料能再抓一次資料嗎

看板AndroidDev作者 (波吉)時間3年前 (2020/05/29 17:54), 編輯推噓1(105)
留言6則, 4人參與, 3年前最新討論串1/1
@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://www.ptt.cc/bbs/AndroidDev/M.1590746090.A.FAB.html

05/29 18:02, 3年前 , 1F
你要開一個thread處理吧,不然你app就anr了
05/29 18:02, 1F

05/29 18:10, 3年前 , 2F
耗時的工作要在另外的執行緒執行, onPostExecute已經在
05/29 18:10, 2F

05/29 18:10, 3年前 , 3F
主執行緒了, 應該要寫在doInBackground
05/29 18:10, 3F

05/29 18:29, 3年前 , 4F
嗯....這個例外的英文不是超級白話嗎= =?
05/29 18:29, 4F

05/31 13:48, 3年前 , 5F
感謝(椅腦s・`)
05/31 13:48, 5F

05/31 13:48, 3年前 , 6F
感謝
05/31 13:48, 6F
文章代碼(AID): #1UqDlg-h (AndroidDev)
文章代碼(AID): #1UqDlg-h (AndroidDev)