android http post 請求與 json字串
阿新 • • 發佈:2019-02-09
List<Entry> items = new ArrayList<Entry>(); // 從response中讀取所有字元格式的返回值 String entityString = EntityUtils.toString(response.getEntity()); // 將字元格式的返回值,對映成Json物件 JSONObject resJsonObj = new JSONObject(entityString); // 嘗試讀取返回的json值中的statusCode欄位 if(resJsonObj.getString(statusCode) == null || resJsonObj.getString(STATUS_CODE).isEmpty() || !resJsonObj.getString(STATUS_CODE).equals("ok")) { return; } // 嘗試解析所有以json陣列形式返回的json物件 JSONArray entiesArray = resJsonObj.getJSONArray("entries");for (int i = 0; i < entiesArray.length(); i++) { JSONObject json = entiesArray.getJSONObject(i); item = new Entry(); item.setName(json.getString("name")); item.setPhonenum(json.getString("phone")); item.setHomePageSites(json.getString("url")); items.add(item); }