Android Studio 获取网络JSON数据并处理
2017-10-11 15:22
477 查看
1、需要的网络JSON数据
2、数据实现类
3、创建List集合接收数据
4、获取JSON数据
2、数据实现类
package chenglong.activitytest.pengintohospital.entity; import org.json.JSONException; import org.json.JSONObject; /** * * 科室 * Created by LICHENGLONG on 2017-10-02. */ public class BasSection { public Integer id;//科室id public String sectionName;//科室名称 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSectionName() { return sectionName; } public void setSectionName(String sectionName) { this.sectionName = sectionName; } public BasSection(Integer id, String sectionName){ this.id = id; this.sectionName = sectionName; } public static BasSection sectionData(JSONObject json){ try { return new BasSection( json.getInt("id"), json.getString("sectionName"); } catch (JSONException e) { e.printStackTrace(); } return null; } }
3、创建List集合接收数据
List<BasSection> listBasSection = new ArrayList<>();
4、获取JSON数据
/** * 获取JSON科室数据 */ public void findSectionData(){ AsyncHttpClient client = new AsyncHttpClient(); //你的JSON数据链接地址 client.get(AbAppConfig.DATA_URL + "appGVConsultation/findSectionData", null, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { try { JSONObject object = new JSONObject(new String(responseBody));//获取json数据 JSONArray jsonArray = object.getJSONArray("obj");//获取数据集名称为obj的数据 Log.d("jsonArray数据输出:", String.valueOf(jsonArray)); for (int i = 0; i < jsonArray.length();i++) { BasSection novels = BasSection.sectionData(jsonArray.getJSONObject(i));//把数据存在novels集合中 if (novels != null){ listBasSection.add(novels); } } } catch (JSONException e) { Toast.makeText(GV_Consultation.this, "数据请求失败,请稍后重试", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { //请求失败的回调处理 Toast.makeText(GV_Consultation.this, "请链接网络,稍后重试", Toast.LENGTH_SHORT).show(); } }); }
相关文章推荐
- Android Studio获取网络JSON数据并处理的方法
- Jquery 使用Ajax获取后台返回的Json数据后,页面处理
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- jquery.post获取处理json数据
- android天气查询(二)之网络json数据的获取
- Android建立网络连接,利用JSON数据获取百度图片搜索结果及GSON的简单使用
- JQuery的ajax获取数据后的处理总结(html,xml,json)
- Swift 2.0 异步网络请求从网页获取json,并转化为NSArray,遍历得到其中数据.
- Android:网络:获取JSON格式数据
- HttpClient获取服务器JSON数据问题处理
- jQuery获取一般处理程序(ashx)的JSON数据
- Android网络编程---从服务器端获取JSON数据
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- HttpClient获取服务器JSON数据问题处理
- 网络中获取json数据
- Antd中Table控件呈现网络获取的json数据
- 从网络接口获取json数据不完全
- Android 网络获取数据之Json解析
- JQuery的ajax获取数据后的处理总结(html,xml,json)
- Android中处理Json的网络获取实例