Android解析json格式的性能比解析XML要高.所以当Android应用请求网络资源时,WEB服务器不返回XML数据,而是json格式的数据.如视频信息Video类的字段为:private Integer id;private String title;private Integer timelength;第一部分:Android客户端(1)Android客户端发送请求public static List<Video> getJSONVideos() throws Exception{String path="http://192.168.1.120:8080/androidStruts/video.do?format=json";URL url=new URL(path);HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();httpURLConnection.setConnectTimeout(5000);httpURLConnection.setRequestMethod("GET");InputStream inputStream=httpURLConnection.getInputStream();if(httpURLConnection.getResponseCode()==200){return parseJSON(inputStream);//parseJSON(InputStream inputStream)方法见下}return null;}(2)Android客户端解析JSON数据步骤:1 将服务器返回的字节数组转换为字符串2 将字符串数据转换为JSON数组——JSONArray jsonArray=new JSONArray(stringVideosData);3 遍历JSON数组,取出数组里的每个元素,其类型JSONObject为——JSONObject jsonObject=jsonArray.getJSONObject(i);4 将每个JSONObject对象的对应值取出来,再填充到对应的JavaBean里面public static List<Video> parseJSON(InputStream inputStream) throws Exception{List<Video> videos=new ArrayList<Video>();byte [] byteVideosData=GetResource.readResource(inputStream);//readResource(InputStream inputStream)方法见下String  stringVideosData=new String(byteVideosData);JSONArray jsonArray=new JSONArray(stringVideosData);for(int i=0;i<jsonArray.length();i++){JSONObject jsonObject=jsonArray.getJSONObject(i);Video video=new Video(jsonObject.getInt("id"),jsonObject.getString("title"),jsonObject.getInt("timelength"));videos.add(video);}return videos;}public class GetResource {    public static byte[] readResource(InputStream inputStream) throws Exception{    ByteArrayOutputStream outputStream=new ByteArrayOutputStream();    byte [] array=new byte[1024];    int len=0;    while( (len=inputStream.read(array))!=-1){       outputStream.write(array,0,len);    }    inputStream.close();    outputStream.close();        return outputStream.toByteArray();    }}第二部分:WEB服务器当Android应用请求网络资源时,WEB服务器发现path中的参数format=json,于是生成JSON数据StringBuilder sbsbJson=new StringBuilder();sbJson.append('[');    for(Video video:videos){    sbJson.append('{');    sbJson.append("id:").append(video.getId()).append(',');    sbJson.append("title:").append(video.getTitle()).append(',');    sbJson.append("timelength:").append(video.getTimelength());    sbJson.append('}').append(',');    }sbJson.deleteCharAt(sbJson.length()-1);//去掉最后一个多余的逗号sbJson.append(']');最终得到:[{id:1,title:犀利哥视频,timelength:45},{id:2,title:福原爱视频,timelength:55},{id:3,title:陈绮贞视频,timelength:65}]Android客户端读到的就是这些JSON格式的数据

更多相关文章

  1. Android系统自带的常用数据库
  2. Android数据的四种存储方式
  3. Android上传文件,客户端+服务器源码
  4. Android 的网络编程(15)-Http JSon服务器端和客户端通信
  5. Android旋转屏幕不销毁数据的方法
  6. Android 博客园客户端 (一) 基本界面
  7. Android使用SQLiteDatabase直接存取数据与图像
  8. android 数据库操作 GreenDAO 第三方开源项目
  9. android 使用Okhttp封装上传JSON格式数据的工具类

随机推荐

  1. android Notification 工具类
  2. Android=》Nitification
  3. android 用代码画圆
  4. Android JNI开发
  5. android AOP编程
  6. android studio 关闭log 打印
  7. Android: Listen outgoing/incoming call
  8. android gpuimage显示的缩放和剪裁模式
  9. Android中的Parcelable接口
  10. Android弹窗