Android---如何利用API实时获取各频道新闻?

基本上万事俱备了,博主我把开发新闻阅读器的相关知识都整理好了,接下来几天我要搞一个新闻阅读器(*^__^*) ~


本次实例包含了利用API(从“百度APIStore”上找的),从网络上实时获取各种频道的新闻。


接下来,我们首先建一个Layout,上面放一个Spinner,用来展现各种新闻频道,而下面,我们放一个TextView,当选中Spinner中相应的频道时,TextView中则会加载从网络获取的该频道实时新闻。

这里先给大家贴个工具类,用来从网络获取信息:

public class UrlUtil {
    //获取频道的网络接口
    public static String channelUrl = "http://apis.baidu.com/showapi_open_bus/channel_news/channel_news";
    //获取频道对应新闻的网络接口
    /*
    get 请求参数
    *channelId 新闻频道id,必须精确匹配
     channelName 新闻频道名称,可模糊匹配
     title 新闻标题,模糊匹配
     page 页数,默认1。每页最多20条记录
    *needContent 是否需要返回正文,1为需要,其他为不需要
    *needHtml 是否需要返回正文的html格式,1为需要,其他为不需要
     */
    public static String newsUrl = "http://apis.baidu.com/showapi_open_bus/channel_news/search_news";

}

Layout:





    
        
            
            
            
        

    




Activity:

public class HttpNewsActivity extends AppCompatActivity {

    private Spinner channel;
    private TextView show;
    private SimpleAdapter sa;
    private List> channelList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_news);
        channel = (Spinner) findViewById(R.id.channel);
        show = (TextView) findViewById(R.id.news);
        channelList = new ArrayList<>();
        sa = new SimpleAdapter(this,channelList,
                android.R.layout.simple_list_item_1,
                new String[]{"name"},new int[]{android.R.id.text1});
        channel.setAdapter(sa);
        String httpUrl = UrlUtil.channelUrl;
        new GetNewsChannel().execute(httpUrl);

        //Spinner监听
        channel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                Map map = channelList.get(position);
                String channelName = (String) map.get("name");
                String channelId = (String) map.get(channelName);
                String[] strings = new String[]{channelId,channelName};
                new GetNews().execute(strings);
            }

            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });
    }

    //获取新闻频道
    public class GetNewsChannel extends AsyncTask{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... strings) {
            return HttpUtil.HttpGet(strings[0]);
        }

        //在UI线程(主线程)中执行命令
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if(s.equals("")){
                Toast.makeText(getBaseContext(),"没有数据",Toast.LENGTH_SHORT).show();
                return;
            }
            try {
                JSONObject obj = new JSONObject(s);
                JSONObject body = obj.getJSONObject("showapi_res_body");
                JSONArray ja = body.getJSONArray("channelList");
                for(int i = 0;i{
        @Override
        protected String doInBackground(String... params) {
            String httpUrl = UrlUtil.newsUrl;
            String httpArg = "channelId="+params[0]+"&" +
                    "channelName="+params[1]+"&" +
                    "title=&" +
                    "page=1&" +
                    "needContent=1&" +
                    "needHtml=1";
            String jsonResult = httpUrl + "?" + httpArg;
            return HttpUtil.HttpGet(jsonResult);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if(s.equals("")){
                show.setText("没有数据或无网络连接");
            }else {
                jsonHandle(s);
            }
        }

    }

    //解析Json
    public void jsonHandle(String s){
        InputStream is ;
        BufferedReader reader = null;
        StringBuilder sbd = new StringBuilder();
        try {
            JSONObject obj = new JSONObject(s);
            JSONObject body = obj.getJSONObject("showapi_res_body");
            JSONObject body2 = body.getJSONObject("pagebean");
            JSONArray ja = body2.getJSONArray("contentlist");
            for(int i = 0;i

预览图:

Android---如何利用API实时获取各频道新闻?_第1张图片

Android---如何利用API实时获取各频道新闻?_第2张图片

Android---如何利用API实时获取各频道新闻?_第3张图片

你可能感兴趣的:(培训日记)