当前位置:主页 > 移动开发 > Android代码 >

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

时间:2020-12-22 17:36:35 | 栏目:Android代码 | 点击:

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

一、创建服务器端:

服务器端项目结构:

服务器端运行效果图:

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

复制代码 代码如下:

 package com.jph.sj.model;
 
import java.util.Date;
 
/**
 * 新闻实体类
 * @author jph
 * Date:2014.09.26
 */
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 {
 
    /**
     * 获取最新的资讯
     * @return
     */
    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 {
    /**
     * 获取最新的视频资讯
     * @return
     */
    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;
 
 
/**
 * 负责响应客户端的请求:http://xxx/NewsListServlet/NewsListServlet
 */
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;
 
/**
 * 获取并解析网络上的Json
 * @author jph
 * Date:2014.09.26
 */
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;
    }
    /**
     * 获取网络上的XML
     */
    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();
        
    }
    /**
     * 解析json格式的输入流转换成List
     * @param inputStream
     * @return List
     */
    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;
    }
    /**
     * 将输入流转化成ByteArray
     * @param inputStream
     * @return ByteArray
     */
    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;
 
/**
 * 获取网络上的Json
 * @author jph
 * Date:2014.09.26
 */
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();
    }
    /**
     * 将解析后的xml填充到ListView
     */
    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运行效果图:

您可能感兴趣的文章:

相关文章