android网络开发网络专题Android开发

网络框架解析与应用

2016-11-14  本文已影响330人  zhangyugehu

网络框架解析与应用

第一章

网络框架完全解析与应用

第二章 网络框架出现背景及优势

http出现及发展

http1.1.png

HTTP协议结构

第三章 网络框架源码结构分析

网络框架源码结构

网络框架简单使用
- 简单实现get、post请求
- // gradle引用

            compile 'com.squareup.okhttp3:okhttp:3.4.2'
    - activity_main.xml
    
            <?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout 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:id="@+id/activity_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context="com.thssh.okhttpstudy.MainActivity">
            
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
            
                    <TextView
                        android:id="@+id/tv_content"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Hello World!" />
                </ScrollView>
            
                <Button
                    android:id="@+id/btn_post"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:clickable="true"
                    android:text="post" />
            
                <Button
                    android:id="@+id/btn_get"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/btn_post"
                    android:layout_alignParentRight="true"
                    android:layout_marginBottom="10dp"
                    android:clickable="true"
                    android:text="get" />
            
            </RelativeLayout>
    - MainActivity.java

            public class MainActivity extends AppCompatActivity {
            
                @Bind(R.id.btn_get)Button btnGet;
                @Bind(R.id.btn_post)Button btnPost;
                @Bind(R.id.tv_content)TextView tvContent;
            
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    ButterKnife.bind(this);
                }
            
                @OnClick(R.id.btn_get)
                public void getRequest(){
                    Request request = new Request.Builder()
                            .url("http://www.baidu.com").build();
                    OkHttpClient client = new OkHttpClient();
                    Call call = client.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            setText(e.getMessage());
                        }
            
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            setText(response.body().string());
                        }
                    });
                }
                @OnClick(R.id.btn_post)
                public void postRequest(){
                    OkHttpClient client = new OkHttpClient();
                    FormBody.Builder form = new FormBody.Builder();
                    form.add("userName", "hutianhang@docmail.cn");
                    form.add("password", "a123456");
                    form.add("clienttype", "mobile");
                    Request req = new Request.Builder()
                            .url("http://mail.docmail.cn/auth/login")
                            .post(form.build())
                            .build();
                    Call call = client.newCall(req);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            setText(e.getMessage());
                        }
            
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            setText(response.body().string());
                        }
                    });
                }
            
                private void setText(final String text){
                    tvContent.post(new Runnable() {
                        @Override
                        public void run() {
                            tvContent.setText(text);
                        }
                    });
                }
            }
- 封装通用网络模块
    - 为什么要封装通用网络模块
        - 强大的可复用性
        - 与业务逻辑完全隔离
        - 强大的可扩展性
    - 封装思路
        - 封装一个公共的OkHttpClient
        - 封装一个通用的请求创建类CommonRequest
        - 封装一个通用的响应解析类JsonCommonRequest
        - 文件下载封装
    - 代码实战通用网络模块封装
    - 使用我们的网络模块

第四章 利用网络框架封装一个通用的网络请求模块

通用网络框架封装(详见源码)

第五章 使用封装好的网络模块

使用实例(详见源码)

第六章 总结

网络框架完全解析与应用

    - HTTP发展及okhttp优势
    - okhttp源码解析及基础用法
    - 基于okhttp封装通用网络模块

大家最爱的部分

上一篇 下一篇

猜你喜欢

热点阅读