<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context="com.example.csdn.MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" app:logo="@mipmap/ic_launcher" app:title="优快云 → 版主推荐-技术区帖子讨论列表" app:titleTextColor="#FFF" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/item_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="@color/colorAccent" android:text="关于ClientWebSocket的异步转同步,我把这个项目搬出来,讲讲清楚" /> <TextView android:id="@+id/item_summary" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="关于ClientWebSocket的异步转同步,我把这个项目搬出来,讲讲清楚" /> <TextView android:id="@+id/item_author" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:text="作者:shawb" /> <ImageView android:layout_width="match_parent" android:layout_height="2dp" android:background="@color/colorPrimary" android:layout_marginTop="10dp"/> </LinearLayout>
package com.example.csdn; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private RecyclerView rv; private LinearLayoutManager manager; private List<EntryBean> mDatas; private EntryAdapter adapter; public String url = "http://bbs.youkuaiyun.com/recommend_tech_topics.atom"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 查找控件 toolbar = (Toolbar) findViewById(R.id.toolbar); rv = (RecyclerView) findViewById(R.id.rv); // 设置布局管理器 manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false); rv.setLayoutManager(manager); // 设置数据源 mDatas = new ArrayList<>(); // 设置适配器 adapter = new EntryAdapter(this,mDatas); rv.setAdapter(adapter); // 获取网络数据 loadWebData(url); } private void loadWebData(final String url) { // 加载网络数据的方法 new AsyncTask<Void,Void,String>(){ @Override protected String doInBackground(Void... params) { String s = HttpUtils.getStringByOkHttp(url); return s; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s!=null) { // 解析数据 List<EntryBean> beanList = ParseXMLData.parseXml(s); // 将网络的集合添加到数据源当中 mDatas.addAll(beanList); // 提示数据更新 adapter.notifyDataSetChanged(); } } }.execute(); } }
package com.example.csdn; 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/3/5. */ public class ParseXMLData { public static List<EntryBean> parseXml(String s){ List<EntryBean>mDatas = new ArrayList<>(); // 1.工厂 2,解析器 3,设置解析数据 4.获取解析码 try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser pullParser = factory.newPullParser(); ByteArrayInputStream baos = new ByteArrayInputStream(s.getBytes()); pullParser.setInput(baos,"utf-8"); int type = pullParser.getEventType(); EntryBean bean = null; while (type != XmlPullParser.END_DOCUMENT) { String name = pullParser.getName(); switch (type) { case XmlPullParser.START_TAG: if (name.equals("entry")) { bean = new EntryBean(); }else if (name.equals("title")) { if (bean!=null) { bean.setTitle(pullParser.nextText()); } }else if (name.equals("summary")) { bean.setSummary(pullParser.nextText()); }else if (name.equals("author")) { bean.setAuthor(pullParser.nextText()); } break; case XmlPullParser.END_TAG: if (name.equals("entry")) { mDatas.add(bean); } break; } type = pullParser.next(); } } catch (Exception e) { e.printStackTrace(); } return mDatas; } }
package com.example.csdn; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import java.util.List; /** * Created by Administrator on 2018/3/5. */ public class EntryAdapter extends RecyclerView.Adapter<EntryAdapter.EntryViewHolder>{ private Context context; private List<EntryBean>mDatas; public EntryAdapter(Context context, List<EntryBean> mDatas) { this.context = context; this.mDatas = mDatas; } @Override public EntryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(context).inflate(R.layout.item_rv,parent,false); EntryViewHolder holder = new EntryViewHolder(itemView); return holder; } @Override public void onBindViewHolder(EntryViewHolder holder, int position) { final EntryBean bean = mDatas.get(position); holder.titleTv.setText(bean.getTitle()); holder.summaryTv.setText(bean.getSummary()); holder.authorTv.setText("作者:"+bean.getAuthor()); // 添加每一项的点击事件 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context,bean.getTitle(),Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return mDatas.size(); } class EntryViewHolder extends RecyclerView.ViewHolder{ TextView titleTv,summaryTv,authorTv; public EntryViewHolder(View itemView) { super(itemView); titleTv = (TextView) itemView.findViewById(R.id.item_title); summaryTv = (TextView) itemView.findViewById(R.id.item_summary); authorTv = (TextView) itemView.findViewById(R.id.item_author); } } }
package com.example.csdn; /** * Created by Administrator on 2018/3/5. * <entry> <id>http://bbs.youkuaiyun.com/topics/392327427</id> <published>2018-03-02T23:04:14+08:00</published> <updated>2018-03-04T12:58:14+08:00</updated> <link rel="alternate" type="text/html" href="http://bbs.youkuaiyun.com/topics/392327427"/> <title>关于ClientWebSocket的异步转同步,我把这个项目搬出来,讲讲清楚</title> <summary>关于ClientWebSocket的异步转同步,我把这个项目搬出来,讲讲清楚</summary> <author>shawb</author> </entry> */ public class EntryBean { private String title; private String summary; private String author; public EntryBean() { } 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 getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
package com.example.csdn; import java.io.IOException; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; /** * Created by Administrator on 2018/3/5. */ public class HttpUtils { public static String getStringByOkHttp(String path){ // 1.客户端对象 OkHttpClient client = new OkHttpClient(); // 2.请求对象 Request request = new Request.Builder().get().url(path).build(); // 3.开始执行请求,获取响应对象 try { Response response = client.newCall(request).execute(); // 4.判断响应是否成功 if (response.isSuccessful()) { // 5.获取响应体内容 ResponseBody body = response.body(); return body.string(); } } catch (IOException e) { e.printStackTrace(); } return null; } }
<uses-permission android:name="android.permission.INTERNET"/>
compile 'com.android.support:recyclerview-v7:26.1.0' compile files('libs/okhttp-3.2.0.jar') compile files('libs/okio-1.8.0.jar')