android针对json数据解析方法实例分析

2019-12-10 19:58:33于海丽
  •     });     }  
  •   private String connServerForResult(String strUrl) {       // HttpGet对象  
  •     HttpGet httpRequest = new HttpGet(strUrl);       String strResult = "";  
  •     try {         // HttpClient对象  
  •       HttpClient httpClient = new DefaultHttpClient();         // 获得HttpResponse对象  
  •       HttpResponse httpResponse = httpClient.execute(httpRequest);         if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
  •         // 取得返回的数据           strResult = EntityUtils.toString(httpResponse.getEntity());  
  •       }       } catch (ClientProtocolException e) {  
  •       tvJson.setText("protocol error");         e.printStackTrace();  
  •     } catch (IOException e) {         tvJson.setText("IO error");  
  •       e.printStackTrace();       }  
  •     return strResult;     }  
  •   // 普通Json数据解析     private void parseJson(String strResult) {  
  •     try {         JSONObject jsonObj = new JSONObject(strResult).getJSONObject("singer");  
  •       int id = jsonObj.getInt("id");         String name = jsonObj.getString("name");  
  •       String gender = jsonObj.getString("gender");         tvJson.setText("ID号"+id + ", 姓名:" + name + ",性别:" + gender);