Android学习之解析JSON
来源:互联网 发布:子曰由汝女知之乎 编辑:程序博客网 时间:2024/06/05 17:21
在Tomcat服务器上放一个json文件
文件内容
[{"id":"01","name":"tiger","age":"12"},{"id":"02","name":"cat","age":"1"},{"id":"03","name":"dog","age":"2"}]Manifest中加入联网许可
<uses-permission android:name="android.permission.INTERNET" />在项目中添加OkHttp库的依赖
compile 'com.squareup.okhttp3:okhttp:3.2.0'compile 'com.squareup.okio:okio:1.6.0'activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.konghao.json.MainActivity"> <ListView android:id="@+id/ListView" android:layout_width="wrap_content" android:layout_height="417dp"/></LinearLayout>item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/name" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/age" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>User类
public class User { private String id; private String name; private String age; public User (String id,String name,String age){ this.id = id; this.name = name; this.age = age; } public String getId(){ return id; } public String getName(){ return name; } public String getAge(){ return age; }}User的适配器,让他能够在ListView中显示
public class UserAdapter extends ArrayAdapter<User> { private int resourceId; public UserAdapter(Context context, int textViewResourceId, List<User> objects){ super(context,textViewResourceId,objects); resourceId = textViewResourceId; } @Override public View getView(int position, View converView, ViewGroup parent){ User user = getItem(position);//获取当前User实例 View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); TextView name = (TextView) view.findViewById(R.id.name); TextView age = (TextView) view.findViewById(R.id.age); name.setText(user.getName()); age.setText(user.getAge()); return view; }}MainActivity.java 解析JSON数据
import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class MainActivity extends AppCompatActivity { private List<User> UserList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); UserAdapter adapter = new UserAdapter(MainActivity.this, R.layout.item,UserList); ListView listView = (ListView) findViewById(R.id.ListView); listView.setAdapter(adapter); initUser(); } //在线程里解析JSON数据 private void initUser() { new Thread(new Runnable() { @Override public void run() { try{ OkHttpClient client = new OkHttpClient();//创建OkHttpClient的实例 Request request = new Request.Builder() //创建一个Request对象,发起一条HTTP请求 .url("http://www.khqust.top/json/user.json")//设置目标网络地址 .build(); Response response = client.newCall(request).execute();//创建一个Call对象发送请求并获取服务器返回的数据 String responseData = response.body().string();//得到具体返回内容 DealData(responseData);//处理返回内容 } catch (IOException e) { e.printStackTrace(); } } }).start(); } private void DealData(String responseData) { try{ JSONArray jsonArray = new JSONArray(responseData); for(int i = 0;i<jsonArray.length();i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); String id = jsonObject.getString("id"); String name = jsonObject.getString("name"); String age = jsonObject.getString("age"); User user = new User(id,name,age); UserList.add(user); } } catch (JSONException e) { e.printStackTrace(); } }}效果:
阅读全文
0 0
- Android学习之解析JSON
- Android学习之解析JSON
- Android学习之JSON数据解析
- Android学习之Json解析库Gson
- Android学习之Json解析库Moshi
- Android 学习之 Json解析 JSONStringer类
- android之Json解析
- Android之JSON解析
- Android之Json解析
- android之json解析
- Android之json解析
- Android 之json解析
- Android之JSON解析
- Android之Json解析
- Android之JSON解析
- android之JSON解析
- Android之解析Json
- Android 之json解析
- leetcode Longest Palindromic Substring
- 1008计算(a+b)/c的值
- Linux日志管理学习
- KMP理解
- mongodb 本地快速复制collection
- Android学习之解析JSON
- 给SSD(固态硬盘)编程
- UVa10600 ACM Contest and Blackout(最小和次小生成树)
- Tarjan求lca
- Unity5使用所遇难点及解决方案(基于与unity4的比较)(一.GUI及导入资源处理)
- 回文数判断
- struts2的OGNL表达式
- Tomcat集群配置笔记
- Linux之redhat通过脚本文件创建对照并重置虚拟机