新闻客户端

来源:互联网 发布:数据产品经理做什么 编辑:程序博客网 时间:2024/05/19 18:39

一、源代码

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;        }    }}


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