本文共 8021 字,大约阅读时间需要 26 分钟。
Python 实现博客服务端,客户端分别 PC Web端 和 手机 Android端
廖雪峰老师
ListView 显示数据
private ListView mListView; private void initValue() { mContext = this; mBlogs = new ArrayList(); mCustomAdapter = new CustomAdapter (mBlogs); mCustomAdapter.setLayoutView(this); mListView.setAdapter(mCustomAdapter); } @Override public View setView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_blog_list, null); holder = new ViewHolder(); holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title); holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } Blogs mBlog = mBlogs.get(position); holder.tv_title.setText(mBlog.name); holder.tv_content.setText(mBlog.summary); return convertView; }
这里使用第三方库 volley.jar
获取应用列表数据
@get('/api/blogs/all')def api_blogs(*, page='1'): blogs = yield from Blog.findAll(orderBy='created_at desc') return dict(blogs=blogs)
=============================================================================// 初始化网络RequestManager.init(this);=============================================================================// 服务端地址,注意下面的描述public class NetConfig { /** * 因为http://127.0.0.1:9000/ 访问PC本地机启动的服务器时会报错, 因为android会默认访问它本身 故使用 * http://10.0.2.2:9000/ */ public static final String HOST_LAVA_POWERSAVE = "http://10.0.2.2:9000"; public static final String URL_BLOG_LIST = HOST_LAVA_POWERSAVE + "/api/blogs/all";============================================================================= // API 接口public class ApiRequests { private static ApiRequests mInstance; public static ApiRequests getInstance() { if (mInstance == null) { mInstance = new ApiRequests(); } return mInstance; } public void getBlogs(Context context, Listener listener, ErrorListener errorListener) { String url = NetConfig.URL_BLOG_LIST; GsonRequestrequest = new GsonRequest (Method.GET, url, BlogsHead.class, (Listener ) listener, errorListener); RequestManager.getRequestQueue().add(request); }}=============================================================================客户端调用 public void updateBlogs() { Log.d(TAG, "updateBlogs"); ApiRequests.getInstance().getBlogs(mContext, new Listener () { @Override public void onResponse(BlogsHead response) { mBlogs.clear(); mBlogs = response.blogs; mHandler.sendEmptyMessage(UPDATE_UI); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d(TAG, error.toString()); } }); }
如下的,看起很凌乱对不对,可以使用 Json格式转换,这样看得懂一些
Json格式化的数据如下
{ "blogs": [{ "id": "001515633814914b02515cf6b284ec1b1eef741d32434ab000", "user_id": "00151558881765135c1d8940a8f4bf19bb2c836eccdb707000", "user_name": "123", "user_image": "http://www.gravatar.com/avatar/b2d7d2d13aed54c2ed7feb538b382b42?d=mm&s=120", "name": "Create Blog", "summary": "Tursday", "content": "Good day", "created_at": 1515630000.0 }, { "id": "0015156358419219e13a79d61584f56bb7242064783e9ff000", "user_id": "00151558881765135c1d8940a8f4bf19bb2c836eccdb707000", "user_name": "123", "user_image": "http://www.gravatar.com/avatar/b2d7d2d13aed54c2ed7feb538b382b42?d=mm&s=120", "name": "sdf", "summary": "sdaf", "content": "asfdasf", "created_at": 1515640000.0 }, { "id": "0015156359616096a8f75b1490348b69857777919992a7d000", "user_id": "00151558881765135c1d8940a8f4bf19bb2c836eccdb707000", "user_name": "123", "user_image": "http://www.gravatar.com/avatar/b2d7d2d13aed54c2ed7feb538b382b42?d=mm&s=120", "name": "Good old days", "summary": "dreams", "content": "We have all got our \"good old days\" tucked away inside our hearts, and we return to them in dreams like cats to favorite armchairs.", "created_at": 1515640000.0 }, { "id": "001515636301092f26271f023994d07863fe52a164781d8000", "user_id": "00151558881765135c1d8940a8f4bf19bb2c836eccdb707000", "user_name": "123", "user_image": "http://www.gravatar.com/avatar/b2d7d2d13aed54c2ed7feb538b382b42?d=mm&s=120", "name": "Good old days", "summary": "dreams", "content": "We have all got our \"good old days\" tucked away inside our hearts, and we return to them in dreams like cats to favorite armchairs.", "created_at": 1515640000.0 }, { "id": "001515758667406801b75382e684a4891b97cb12b566760000", "user_id": "00151558881765135c1d8940a8f4bf19bb2c836eccdb707000", "user_name": "123", "user_image": "http://www.gravatar.com/avatar/b2d7d2d13aed54c2ed7feb538b382b42?d=mm&s=120", "name": "Hello world", "summary": "Hello", "content": "Python study", "created_at": 1515760000.0 }, { "id": "1", "user_id": "1", "user_name": "123", "user_image": "safdsadfsdf", "name": "234", "summary": "adsffasdf", "content": "fasdfsadfsa", "created_at": 123123000.0 }]}
这里使用 GSON
根据上述的数据,定义下面的类=========================================================package com.su.androidblog.bean;import java.util.List;public class BlogsHead { public Listblogs;}根据上述的数据,定义下面的类=========================================================package com.su.androidblog.bean;public class Blogs { /* * "blogs": [{ "id": "001515758667406801b75382e684a4891b97cb12b566760000", * "user_id": "00151558881765135c1d8940a8f4bf19bb2c836eccdb707000", * "user_name": "123", "user_image": * "http://www.gravatar.com/avatar/b2d7d2d13aed54c2ed7feb538b382b42?d=mm&s=120" * , "name": "Hello world", "summary": "Hello", "content": "Python study", * "created_at": 1515760000.0 */ public String id; public String user_id; public String user_name; public String user_image; public String name; public String summary; public String content; public String created_at;}=========================================================ApiRequests.java public void getBlogs(Context context, Listener listener, ErrorListener errorListener) { String url = NetConfig.URL_BLOG_LIST; GsonRequest request = new GsonRequest (Method.GET, url, BlogsHead.class, (Listener ) listener, errorListener); RequestManager.getRequestQueue().add(request); }========================================================= ApiRequests.getInstance().getBlogs(mContext, new Listener () { @Override public void onResponse(BlogsHead response) { mBlogs.clear(); mBlogs = response.blogs; mHandler.sendEmptyMessage(UPDATE_UI); }=========================================================
mHandler.sendEmptyMessage(UPDATE_UI); private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case UPDATE_UI: mCustomAdapter.updateData((ArrayList) mBlogs); break; default: break; } } };
本项目的 GitHub
Python 应该还有其他用途吧,打算学下 Python 写的爬虫项目