您的位置:首页 > 理论基础 > 计算机网络

实时更新XML文件获取网络数据

2018-01-18 15:05 337 查看
package com.animee.day21.demo02;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.animee.day21.R;

import java.util.List;

/**
* Created by Administrator on 2018/1/18.
*/

public class BlogAdapter extends BaseAdapter{
private Context context;
private List<BlogBean.Entry>mDatas;

public BlogAdapter(Context context, List<BlogBean.Entry> mDatas) {
this.context = context;
this.mDatas = mDatas;
}

@Override
public int getCount() {
return mDatas.size();
}

@Override
public Object getItem(int position) {
return mDatas.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView==null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_lv_demo02,null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
BlogBean.Entry entry = mDatas.get(position);
holder.titleTv.setText(entry.getTitle());
holder.summaryTv.setText(entry.getSummary());
holder.authorTv.setText("作者:"+entry.getName());
holder.timeTv.setText(entry.getPublished());
return convertView;
}

class ViewHolder{
TextView titleTv,summaryTv,authorTv,timeTv;
public ViewHolder(View view){
titleTv = (TextView) view.findViewById(R.id.item_tv_title);
summaryTv = (TextView) view.findViewById(R.id.item_tv_summary);
authorTv = (TextView) view.findViewById(R.id.item_tv_author);
timeTv = (TextView) view.findViewById(R.id.item_tv_time);
}
}
}
package com.animee.day21.demo02;

import java.util.List;

/**
* Created by Administrator on 2018/1/18.
* <id>http://www.cnblogs.com/zhangxufeng/p/8308558.html</id>
<title type="text">数据库索引创建与优化 - 爱宝贝丶</title>
<summary type="text">
对于数据库的优化主要包括三个部分:查询优化、索引优化和字段类型优化,其中,索引优化则是数据库优化的重中之重。一个查询使用索引与不使用索引的差别可能只在100个数量级,而一个好的索引与不好的索引差别可能在1000个数量
</summary>
<published>2018-01-18T01:24:00Z</published>
<updated>2018-01-18T01:24:00Z</updated>
<author>
<name>爱宝贝丶</name>
<uri>http://www.cnblogs.com/zhangxufeng/</uri>
</author>
<link rel="alternate" href="http://www.cnblogs.com/zhangxufeng/p/8308558.html"/>
<link rel="alternate" type="text/html" href="http://www.cnblogs.com/zhangxufeng/p/8308558.html"/>
<content type="html">
【摘要】 对于数据库的优化主要包括三个部分:查询优化、索引优化和字段类型优化,其中,索引优化则是数据库优化的重中之重。一个查询使用索引与不使用索引的差别可能只在100个数量级,而一个好的索引与不好的索引差别可能在1000个数量 <a href="http://www.cnblogs.com/zhangxufeng/p/8308558.html" target="_blank">阅读全文</a>
</content>
*/

public class BlogBean {

private List<Entry>entryList;

public List<Entry> getEntryList() {
return entryList;
}

public void setEntryList(List<Entry> entryList) {
this.entryList = entryList;
}

public BlogBean(List<Entry> entryList) {
this.entryList = entryList;
}

public BlogBean() {
}

public static class Entry{
private String id;
private String title;
private String summary;
private String published;
private String updated;
private String name;

@Override
public String toString() {
return "Entry{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", summary='" + summary + '\'' +
", published='" + published + '\'' +
", updated='" + updated + '\'' +
", name='" + name + '\'' +
'}';
}

public Entry() {
}

public Entry(String id, String title, String summary, String published, String updated, String name) {
this.id = id;
this.title = title;
this.summary = summary;
this.published = published;
this.updated = updated;
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getTitle() {
return title;
}

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

public String getSummary() {
return summary;
}

public void setSummary(String summary) {
this.summary = summary;
}

public String getPublished() {
return published;
}

public void setPublished(String published) {
this.published = published;
}

public String getUpdated() {
return updated;
}

public void setUpdated(String updated) {
this.updated = updated;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}

package com.animee.day21.demo02;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.animee.day21.MyApp;
import com.animee.day21.R;

import java.util.ArrayList;
import java.util.List;

public class DemoActivity02 extends AppCompatActivity {
public String TAG = "DemoActivity02";
private ListView lv;
private List<BlogBean.Entry> entryList;
private BlogAdapter adapter;
private String url = "http://feed.cnblogs.com/blog/sitehome/rss";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo02);
lv = (ListView) findViewById(R.id.lv_demo02);
//        数据源
entryList = new ArrayList<>();
//        适配器
adapter = new BlogAdapter(this,entryList);
lv.setAdapter(adapter);

//        loadWebData(url);
loadData(url);

}
private void loadData(final String url){
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... params) {
String content = HttpUtils.getStringContent(url);
return content;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s!=null&&!s.isEmpty()) {
Log.i(TAG, "onPostExecute: ===s=="+s);
BlogBean blogBean = ParseXML.readXML(s);
entryList.addAll(blogBean.getEntryList());
adapter.notifyDataSetChanged();
}
}
}.execute();
}

/**
* 使用volley获取的数据:出现了问题
* */
private void loadWebData(String url) {
//        1.请求队列
//        2.请求对象
StringRequest request =new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.i(TAG, "onResponse: s===="+s);
//                解析数据,并展示
BlogBean blogBean = ParseXML.readXML(s);
entryList.addAll(blogBean.getEntryList());
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onError
4000
Response(VolleyError volleyError) {
Log.i(TAG, "onErrorResponse: error====="+volleyError.toString());
}
});
//        3.添加请求对象到请求队列当中
MyApp.getHttpQueue().add(request);
}
}

package com.animee.day21.demo02;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* Created by Administrator on 2018/1/18.
*/

public class HttpUtils {
public static String getStringContent(String path){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
InputStream is = conn.getInputStream();
int hasRead = 0;
byte[]buf = new byte[1024];
while((hasRead = is.read(buf))!=-1){
baos.write(buf,0,hasRead);
}
} catch (Exception e) {
e.printStackTrace();
}
return baos.toString();
}
}
package com.animee.day21.demo02;

import android.util.Log;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

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

/**
* Created by Administrator on 2018/1/18.
*/

public class ParseXML {
public static String TAG = "ParseXML";
public static BlogBean readXML(String content){
BlogBean blogBean = new BlogBean();
List<BlogBean.Entry>entryList = new ArrayList<>();
blogBean.setEntryList(entryList);
//        开始解析
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = factory.newPullParser();
ByteArrayInputStream stream = new ByteArrayInputStream(content.getBytes());
pullParser.setInput(stream,"utf-8");
int eventType = pullParser.getEventType();
BlogBean.Entry entry = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
String name = pullParser.getName();
Log.i(TAG, "readXML: ==name=="+name);
switch (eventType) {
case XmlPullParser.START_TAG:
if (name.equals("entry")) {
entry = new BlogBean.Entry();
}else if (name.equals("id")) {
if (entry!=null) {
entry.setId(pullParser.nextText());
}
}else if (name.equals("title")) {
if (entry!=null) {
entry.setTitle(pullParser.nextText());
}
}else if (name.equals("summary")) {
entry.setSummary(pullParser.nextText());
}else if (name.equals("published")) {
entry.setPublished(pullParser.nextText());
}else if (name.equals("updated")) {
if (entry!=null) {
entry.setUpdated(pullParser.nextText());
}
}else if (name.equals("name")) {
entry.setName(pullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if (name.equals("entry")) {
entryList.add(entry);
}
break;
}
eventType = pullParser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
Log.i(TAG, "readXML: ===list.size=="+entryList.size());
return blogBean;
}
}
package com.animee.day21;

import android.app.Application;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

/**
* Created by Administrator on 2018/1/18.
*/

public class MyApp extends Application{
//  第一步:声明请求队列对象
private static RequestQueue queue;
@Override
public void onCreate() {
super.onCreate();
queue = Volley.newRequestQueue(getApplicationContext());
}

public static RequestQueue getHttpQueue(){
return queue;
}
}

布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<TextView
android:id="@+id/item_tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="【算法】赫夫曼树(Huffman)的构建和应用(编码、译码) - 外婆的彭湖湾"
android:textSize="20sp"/>
<TextView
android:id="@+id/item_tv_summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/item_tv_title"
android:layout_alignLeft="@+id/item_tv_title"
android:layout_marginTop="10dp"
android:padding="10dp"
android:textColor="@color/colorPrimary"
android:background="#eee"
android:text="【算法】赫夫曼树(Huffman)的构建和应用(编码、译码) - 外婆的彭湖湾"
android:textSize="16sp"/>

<TextView
android:id="@+id/item_tv_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/item_tv_summary"
android:layout_alignLeft="@+id/item_tv_title"
android:layout_marginTop="10dp"
android:text="作者:赫夫曼"
android:textSize="16sp"/>

<TextView
android:id="@+id/item_tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/item_tv_summary"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:text="2018-01-17T22:19:00Z"
android:textSize="16sp"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">

<TextView
android:id="@+id/item_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="18sp"
android:ellipsize="end"
android:singleLine="true"
android:textColor="@color/colorPrimary"/>

<ImageView
android:id="@+id/item_iv"
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: