返回顶部
首页 > 资讯 > 移动开发 >Android网络编程之获取网络上的Json数据实例
  • 215
分享到

Android网络编程之获取网络上的Json数据实例

程之json数据JSONAndroid 2022-06-06 10:06:09 215人浏览 八月长安
摘要

为要获取网络上的JSON所以需要服务器端提供的支持。 一、创建服务器端: 服务器端项目结构: 服务器端运行效果图: 第一步:创建业务所需的JavaBean 代码如下: &

为要获取网络上的JSON所以需要服务器端提供的支持。

一、创建服务器端:

服务器端项目结构:

服务器端运行效果图:

第一步:创建业务所需的JavaBean

代码如下:
 package com.jph.sj.model;
 
import java.util.Date;
 

public class News {
    private Integer id;
    private String title;
    private Date publishDate;
    
    public News(Integer id, String title, Date publishDate) {
        this.id = id;
        this.title = title;
        this.publishDate = publishDate;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Date getPublishDate() {
        return publishDate;
    }
    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }
}

第二步:创建业务逻辑接口和具体实现类

业务接口:

代码如下:
package com.jph.sj.service;
 
import java.util.List;
 
import com.jph.sj.model.News;
 
 
public interface NewsService {
 
   
    public List<News> getLastNews();
 
}

业务接口的实现类:

代码如下:
  package com.jph.sj.service.impl;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.jph.sj.model.News;
import com.jph.sj.service.NewsService;
 
public class NewsServiceBean implements NewsService {
   
    public List<News> getLastNews(){      
        List<News> newes = new ArrayList<News>();
        newes.add(new News(1, "李白", new Date(System.currentTimeMillis())));
        newes.add(new News(2, "杜甫", new Date(System.currentTimeMillis()+8200)));
        newes.add(new News(3, "贾宝玉",new Date(System.currentTimeMillis()-6000)));
        return newes;
    }
}

第三步:创建控制器Servlet

代码如下:
 package com.jph.sj.servlet;
 
import java.io.IOException;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.Http.httpservlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.jph.sj.model.News;
import com.jph.sj.service.NewsService;
import com.jph.sj.service.impl.NewsServiceBean;
 
 

public class NewsListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private NewsService newsService = new NewsServiceBean();
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<News> newes = newsService.getLastNews();//获取最新的视频资讯
            //[{id:20,title:"xxx",timelength:90},{id:10,title:"xbx",timelength:20}]
            StringBuilder sbjson = new StringBuilder();
            //将list集合封装成Json格式的字符串
            sbJson.append('[');
            for(News news : newes){
                sbJson.append('{');
                sbJson.append("id:").append(news.getId()).append(",");
                sbJson.append("title:\"").append(news.getTitle()).append("\",");
                sbJson.append("publishDate:").append(news.getPublishDate().getTime());
                sbJson.append("},");
            }
            sbJson.deleteCharAt(sbJson.length() - 1);//删除字符串末尾的逗号
            sbJson.append(']');
            request.setAttribute("json", sbJson.toString());
            request.getRequestDispatcher("/WEB-INF/page/jsonnewslist.jsp").forward(request, response);
    }
}

第四步:创建jsonnewslist.jsp页面

代码如下:
<%@ page language="java" contentType="text/plain; charset=UTF-8" pageEncoding="UTF-8"%>
${json}

至此服务器端项目已经完成。下面开始创建Android端项目。

二、创建Android端:

Android端项目结构:

第一步:创建业务所需的JavaBean

提示:因为服务器端和Android端项目都是用Java语言实现的,所以一些组件可以共用,JavaBean便是其中之一。此时我们在搭建Android端项目的时候,完全可以将服务器端项目中JavaBean拿来用。

第二步:创建Android端项目的业务逻辑层

核心代码:GetAndParseJson:
代码如下:
  package com.jph.gj.service;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.jph.model.News;
import android.os.Handler;
import android.os.Message;
 

public class GetAndParseJson {
    private String url="http://10.219.61.117:8080/ServerForJSON/NewsListServlet"; 
    public static final int PARSESUCCWSS=0x2001;
    private Handler handler;
    public GetAndParseJson(Handler handler) {
        // TODO Auto-generated constructor stub
        this.handler=handler;
    }
   
    public void getJsonFromInternet () {
        new Thread(new Runnable() {       
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    HttpURLConnection conn=(HttpURLConnection) new URL(url).openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setRequestMethod("GET");
                    if (conn.getResponseCode()==200) {
                        InputStream inputStream=conn.getInputStream();                    
                        List<News>listNews=parseJson(inputStream);
                        if (listNews.size()>0) {
                            Message msg=new Message();
                            msg.what=PARSESUCCWSS;//通知UI线程Json解析完成
                            msg.obj=listNews;//将解析出的数据传递给UI线程
                            handler.sendMessage(msg);
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }             
            }
        }).start();
        
    }
   
    protected List<News> parseJson(InputStream inputStream) {
        // TODO Auto-generated method stub
        List<News>listNews=new ArrayList<News>();
        byte[]jsonBytes=convertIsToByteArray(inputStream);
        String json=new String(jsonBytes);
        try {
            JSONArray jsonArray=new JSONArray(json);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jObject=jsonArray.getJSONObject(i);
                int id=jObject.getInt("id");
                String title=jObject.getString("title");          
                long time=jObject.getLong("publishDate");
                News news=new News(id, title, new Date(time));
                listNews.add(news);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return listNews;
    }
   
    private byte[] convertIsToByteArray(InputStream inputStream) {
        // TODO Auto-generated method stub    
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        byte buffer[]=new byte[1024];
        int length=0;
        try {
            while ((length=inputStream.read(buffer))!=-1) {
                baos.write(buffer, 0, length);            
            }
            inputStream.close();
            baos.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return baos.toByteArray();
    }
}

第三步:创建Activity

代码如下:
package com.jph.gj.activity;
 
import java.text.SimpleDateFORMat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jph.gj.R;
import com.jph.gj.service.GetAndParseJson;
import com.jph.model.News;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
 

public class MainActivity extends Activity {
    private List<News>listNews;
    private ListView list;
    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
            case GetAndParseJson.PARSESUCCWSS:
                listNews=(List<News>) msg.obj;
                initData();
                break;        
            }
            super.handleMessage(msg);
        }     
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list=(ListView)findViewById(R.id.list);
        GetAndParseJson getAndParseJson=new GetAndParseJson(mHandler);
        getAndParseJson.getJsonFromInternet();
    }
   
    protected void initData() {
        // TODO Auto-generated method stub
        List<Map<String, Object>>items=new ArrayList<Map<String,Object>>();
        for (News news:listNews) {
            Map<String, Object>item=new HashMap<String, Object>();
            item.put("id", news.getId());
            item.put("title", news.getTitle());
            item.put("time", convertDate(news.getPublishDate()));
            items.add(item);
        }
        SimpleAdapter adapter=new SimpleAdapter(this, items, R.layout.line, new String[]
                {"id","title","time"}, new int[]{R.id.tvId,R.id.tvTitle,R.id.tvTime});
        list.setAdapter(adapter);
    }
    private String convertDate(Date publishDate) {
        // TODO Auto-generated method stub
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");     
        return sdf.format(publishDate);
    }
}

至此Android端项目已经完成了。下面就让我们看一下APP运行效果吧:
Android运行效果图:

您可能感兴趣的文章:android 网络编程之网络通信几种方式实例分享Android中Retrofit+OkHttp进行HTTP网络编程的使用指南Android网络编程之UDP通信模型实例android网络编程之android连接网络的简单示例代码Android通过HttpURLConnection和HttpClient接口实现网络编程Android网络编程之简易新闻客户端Android中实现tcp和UDP传输实例Android使用Socket创建简单TCP连接的方法Android调用默认浏览器打开指定Url的方法实例Android基于TCP和URL协议的网络编程示例【附demo源码下载】


--结束END--

本文标题: Android网络编程之获取网络上的Json数据实例

本文链接: https://lsjlt.com/news/26981.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作