新闻客户端

一、源代码

package cn.edu.bzu.newsclient;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.image.SmartImageView;

import org.apache.http.Header;

import java.io.ByteArrayInputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView lv_news;
    private LinearLayout loading;
    private List
   
   
    
     newsInfos;

    private class NewsAdapter extends BaseAdapter {
        public int getCount() {
            return newsInfos.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this, R.layout.news_item, null);
            SmartImageView siv = (SmartImageView) view.findViewById(R.id.siv_icon);
            TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
            TextView tv_description = (TextView) view.findViewById(R.id.tv_description);
            TextView tv_type = (TextView) view.findViewById(R.id.tv_type);
            NewsInfo newsInfo = newsInfos.get(position);
            siv.setImageUrl(newsInfo.getIconPath(), R.mipmap.ic_launcher, R.mipmap.ic_launcher);
            tv_title.setText(newsInfo.getTitle());
            tv_description.setText(newsInfo.getDescription());
            int type = newsInfo.getType();
            switch (type) {
                case 1:
                    tv_type.setText("评论:" + newsInfo.getComment());
                    break;
                case 2:
                    tv_type.setTextColor(Color.RED);
                    tv_type.setText("专题");
                    break;
                case 3:
                    tv_type.setTextColor(Color.BLUE);
                    tv_type.setText("LIVE");
                    break;
            }
            return view;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_news = (ListView) findViewById(R.id.lv_news);
        loading = (LinearLayout) findViewById(R.id.loading);
        fillData2();

    }

    private void fillData2() {
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.get(getString(R.string.serverurl),
                new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(String content) {
                        super.onSuccess(content);
                        byte[] bytes = content.getBytes();
                        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                        newsInfos = NewsInfoService.getNewsInfos(bais);
                        if (newsInfos == null) {
                            Toast.makeText(MainActivity.this, "解析失败", Toast.LENGTH_SHORT).show();
                        } else {
                            loading.setVisibility(View.INVISIBLE);
                            lv_news.setAdapter(new NewsAdapter());
                        }
                    }

                    @Override
                    public void onFailure(Throwable error, String content) {
                        super.onFailure(error, content);
                        Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}

package cn.edu.bzu.newsclient;

/**
 * Created by zhangaozhi on 2017/5/24.
 */

public class NewsInfo {
    private String iconPath;
    private String title;
    private String description;
    private int type;
    private long comment;

    public String getIconPath() {
        return iconPath;
    }

    public void setIconPath(String iconPath) {
        this.iconPath = iconPath;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public long getComment() {
        return comment;
    }

    public void setComment(long comment) {
        this.comment = comment;
    }
}
package cn.edu.bzu.newsclient;

import android.util.Xml;
import android.widget.Switch;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static cn.edu.bzu.newsclient.R.drawable.e;

/**
 * Created by zhangaozhi on 2017/5/24.
 */

public class NewsInfoService {
    private static List
    
    
     
      getNewsInfos(InputStream is) throws IOException {
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(is, "utf-8");
            int type = parser.getEventType();
            List
     
     
      
       newsInfos = null;
            NewsInfo newsInfo = null;
            while (type != XmlPullParser.END_DOCUMENT) {
                switch (type) {
                    case XmlPullParser.START_TAG:
                        if ("news".equals(parser.getName())) {
                            newsInfos = new ArrayList
      
      
       
       ();
                        } else if ("newsInfo".equals(parser.getName())) {
                            newsInfo = new NewsInfo();
                        } else if ("icon".equals(parser.getName())) {
                            String icon = parser.nextText();
                            newsInfo.setIconPath(icon);
                        } else if ("title".equals(parser.getName())) {
                            String title = parser.nextText();
                            newsInfo.setTitle(title);
                        } else if ("content".equals(parser.getName())) {
                            String description = parser.nextText();
                            newsInfo.setTitle(description);
                        } else if ("type".equals(parser.getName())) {
                            String newsType = parser.nextText();
                            newsInfo.setTitle(newsType);
                        } else if ("comment".equals(parser.getName())) {
                            String comment = parser.nextText();
                            newsInfo.setTitle(comment);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("newsInfo".equals(parser.getName())) {
                            newsInfos.add(newsInfo);
                            newsInfo = null;
                        }
                        break;
                }
                type = parser.next();
            }
            return newsInfos;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
      
      
     
     
    
    
   
   


由于条件不允许没有结果图。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值