博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 基础-项目实战-day 13 Android客户端开发(项目完结)
阅读量:4150 次
发布时间:2019-05-25

本文共 8021 字,大约阅读时间需要 26 分钟。

一、项目介绍

Python 实现博客服务端,客户端分别 PC Web端 和 手机 Android端

1.项目来源

廖雪峰老师

2.项目 Github

3.运行效果

Android 客户端

二、Android 端实现

1.建立一个简单的 ListView 展示

1.1 布局

ListView 显示数据

1.2 List的初始化

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

2.Python 数据获取

这里使用第三方库 volley.jar

2.1 Python 服务端 API 接口

获取应用列表数据

这里写图片描述

@get('/api/blogs/all')def api_blogs(*, page='1'):    blogs = yield from Blog.findAll(orderBy='created_at desc')    return dict(blogs=blogs)

2.2 volley 的使用和网络接口

=============================================================================// 初始化网络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; GsonRequest
request = 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()); } }); }

2.1 数据结果

如下的,看起很凌乱对不对,可以使用 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    }]}

3 解析数据库

这里使用 GSON

根据上述的数据,定义下面的类=========================================================package com.su.androidblog.bean;import java.util.List;public class BlogsHead {    public List
blogs;}根据上述的数据,定义下面的类=========================================================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); }=========================================================

4 显示数据

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

5 小结

本项目的 GitHub

Python 应该还有其他用途吧,打算学下 Python 写的爬虫项目

你可能感兴趣的文章
HDU 1271 整数对(思路题)
查看>>
HDU 4116 Fruit Ninja ( 计算几何 + 扫描线 )
查看>>
FD.io 社区中国行暨未来网络技术沙龙 南京站 参会小结
查看>>
使用jdbc编程问题总结
查看>>
Fiddler (三) Composer创建和发送HTTP Request
查看>>
【数据结构】链表
查看>>
Android点阵屏效果的控件
查看>>
使用dom4j工具包对xml文件解析
查看>>
规范化Normalization
查看>>
centos安装ruby on rails
查看>>
java Resource
查看>>
AIS (Automatic Identification System)船舶自动识别系统
查看>>
PIE SDK正射校正
查看>>
PIE SDK线元素的绘制
查看>>
总结3
查看>>
爬虫基础——HTTP基本原理
查看>>
Android 第三方内容分享旧版
查看>>
VMware vCenter Converter 使用,Error code: 2147549183 (0x8000FFFF) 解决方案
查看>>
【转】Docker基础
查看>>
乱码问题
查看>>