Android 搭建网络访问框架(下)
下面进入使用环节了,回到app模块。
八、使用网络框架目前app模块下只有这一个孤零零的MainActivity。首先在app下的com.llw.network下新建一个application包,(在实际开发中尽量要避免包名重复的情况),在这个包下创建一个NetworkRequiredInfo类,然后实现network模块下的INetworkRequiredInfo接口。
你会发现,这个报红,这时因为你没有添加network模块的依赖,那么有三种方式可以添加,
1. 添加网络模块依赖
① 当前项目添加
第一种:
鼠标点击这个报红处,然后使用Alt+Enter,会出现如下弹窗,点击第一项Add dependency on module ‘network’,意思就是添加network模块的依赖。
点击之后,等待即可然后发现报错了,这个报错是为什么呢?
你打开app的build.gradle就知道了,如下图所示:
我这里解释一下是为什么,随着Gradle版本的更新,以前的一些使用方式就弃用了,比如这个compile就过时了,因此在高版本中可以替换为implementation和api。那么将compile替换成为implementation之后点击右上角的Sync Now进行同步。
这样就编译成功了,上面通过Alt + Enter的方式虽然不用我们改动,但是这个内部机制还是低版本的,它做的无非就是加一句话而已,那么我们也可以自己来加不是吗?
第二种:
打开app的build.gradle,在dependencies{}闭包下添加如下依赖:
//依赖网络模块
implementation project(path: ':network')
注意这个格式,所有的标点符号都是因为英文的,network对应你的模块的名字,它前面还有一个英文冒号。
然后就点击Sync Now同步就可以了。
第三种:
通过File→Project Structure…
或者点击这个图标都会进入如下页面然后通过下图这四步操作即可添加这个模块依赖。然后勾选上,下面的下拉框中可以选择类型。可以看到高版本AS中已经没有compile的选项了,点击OK即可。再点击OK,然后你打开app的build.gradle查看,里面一定多了一个依赖,如下图所示:
这种方式可以把错误和修改的可能性降到最低,推荐使用。
② 其他项目或新项目添加
同样你假如要在一个新的项目中使用这个network模块也可以这么做。比如我打开我之前写的关于高德地图的项目Demo。里面没有网络模块,因此需要先导入模块才行,通过File→New→Import Module…点击后出现找到之前的模块所在路径。然后点击Finish
然后你就可以通过上面的第三步进行添加依赖了。
这对于不熟悉的朋友来说还是不错的,因为有时候他们配置项目时会出现各种各样的问题,五花八门,最终就是表现为报错了,然后不知道为什么报错,因此详细一点也没有错。
OK下面进入当前项目的使用
2. 使用网络模块
上面由一个NetworkRequiredInfo引发出这么多内容,但是我觉得是有必要讲一下的,也是做一个笔记吧,那么回到这个NetworkRequiredInfo中
你可以看到现在你就可以导包,然后使用这个INetworkRequiredInfo,导包也是使用Alt+Enter快捷键,如果你这个接口是唯一的,则会直接导包,如果不是唯一的则会出现一个弹窗供你选择要导入的包,所属,还记得上面使用Observer的时候吗?它就是不唯一的,androidx.lifecyle下有,io.reactivex下也有,你要是导错了包,那么后面怎么搞都是有问题的,这都是细节,需要注意才行。
实现里面的方法,最终里面的代码如下:
package com.llw.network.application;
import android.app.Application;
import com.llw.network.BuildConfig;
import com.llw.network.INetworkRequiredInfo;
/**
* 网络访问信息
* @author llw
*/
public class NetworkRequiredInfo implements INetworkRequiredInfo {
private Application application;
public NetworkRequiredInfo(Application application){
this.application = application;
}
/**
* 版本名
*/
@Override
public String getAppVersionName() {
return BuildConfig.VERSION_NAME;
}
/**
* 版本号
*/
@Override
public String getAppVersionCode() {
return String.valueOf(BuildConfig.VERSION_CODE);
}
/**
* 是否为debug
*/
@Override
public boolean isDebug() {
return BuildConfig.DEBUG;
}
/**
* 应用全局上下文
*/
@Override
public Application getApplicationContext() {
return application;
}
}
相信应该很好理解,然后在这个application包下再创建一个MyApplication类,继承Application。重写onCreate方法,在里面完成对NetworkApi和NetworkRequiredInfo的初始化配置,里面的代码如下:
package com.llw.network.application;
import android.app.Application;
import com.llw.network.NetworkApi;
/**
* 自定义Application
* @author llw
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化
NetworkApi.init(new NetworkRequiredInfo(this));
}
}
然后打开AndroidManifest.xml中进行配置
在这里配置自定义的Application。
下面就要来显示数据了,
https://gank.io/api/v2/data/category/Girl/type/Girl/page/1/count/10
可以在浏览器数据下面的这个地址,然后会返回如下JSON数据:
{"data":[{"_id":"5e959250808d6d2fe6b56eda","author":"\u9e22\u5a9b","category":"Girl","createdAt":"2020-05-25 08:00:00","desc":"\u4e0e\u5176\u5b89\u6170\u81ea\u5df1\u5e73\u51e1\u53ef\u8d35\uff0c\n\u4e0d\u5982\u62fc\u5c3d\u5168\u529b\u6d3b\u5f97\u6f02\u4eae\u3002 \u200b \u200b\u200b\u200b\u200b","images":["http://gank.io/images/f4f6d68bf30147e1bdd4ddbc6ad7c2a2"],"likeCounts":6,"publishedAt":"2020-05-25 08:00:00","stars":1,"title":"\u7b2c96\u671f","type":"Girl","url":"http://gank.io/images/f4f6d68bf30147e1bdd4ddbc6ad7c2a2","views":11898},{"_id":"5e95923f808d6d2fe6b56ed8","author":"\u9e22\u5a9b","category":"Girl","createdAt":"2020-05-24 08:00:00","desc":"\u8fd9\u4e16\u754c\u603b\u6709\u4eba\u5728\u7b28\u62d9\u5730\u7231\u7740\u4f60\uff0c\u60f3\u628a\u5168\u90e8\u7684\u6e29\u67d4\u90fd\u7ed9\u4f60\u3002 \u200b\u200b\u200b\u200b","images":["http://gank.io/images/dc75cbde1d98448183e2f9514b4d1320"],"likeCounts":3,"publishedAt":"2020-05-24 08:00:00","stars":1,"title":"\u7b2c95\u671f","type":"Girl","url":"http://gank.io/images/dc75cbde1d98448183e2f9514b4d1320","views":5085},{"_id":"5e95922e808d6d2fe6b56ed6","author":"\u9e22\u5a9b","category":"Girl","createdAt":"2020-05-23 08:00:00","desc":"\u966a\u4f34\u672c\u6765\u5c31\u662f\u8fd9\u4e16\u754c\u4e0a\u6700\u4e86\u4e0d\u8d77\u7684\u5b89\u6170\u200b\u3002","images":["http://gank.io/images/6b2efa591564475fb8bc32291fb0007c"],"likeCounts":1,"publishedAt":"2020-05-23 08:00:00","stars":1,"title":"\u7b2c94\u671f","type":"Girl","url":"http://gank.io/images/6b2efa591564475fb8bc32291fb0007c","views":4480},{"_id":"5e959200ee6ba981da2af34d","author":"\u9e22\u5a9b","category":"Girl","createdAt":"2020-05-22 08:00:00","desc":"\u957f\u4e0d\u8fc7\u6267\u5ff5\uff0c\u77ed\u4e0d\u8fc7\u5584\u53d8\u3002","images":["http://gank.io/images/d6bba8cf5b8c40f9ad229844475e9149"],"likeCounts":2,"publishedAt":"2020-05-22 08:00:00","stars":1,"title":"\u7b2c93\u671f","type":"Girl","url":"http://gank.io/images/d6bba8cf5b8c40f9ad229844475e9149","views":3589},{"_id":"5e9591dcee6ba981da2af34b","author":"\u9e22\u5a9b","category":"Girl","createdAt":"2020-05-21 08:00:00","desc":"\u65e0\u8bba\u591a\u4e48\u8270\u96be\u7684\u73b0\u5728\uff0c\u7ec8\u4f1a\u7ffb\u7bc7\u3002\n\u671d\u672a\u6765\u5927\u6b65\u5411\u524d\u5427\uff0c\u522b\u4e27\uff0c\u522b\u6b62\u6b65\u3002","images":["http://gank.io/images/9fa43020cf724c69842eec3e13f6d21c"],"likeCounts":4,"publishedAt":"2020-05-21 08:00:00","stars":1,"title":"\u7b2c92\u671f","type":"Girl","url":"http://gank.io/images/9fa43020cf724c69842eec3e13f6d21c","views":2434},{"_id":"5e9591c60bd5529b54e712af","author":"\u9e22\u5a9b","category":"Girl","createdAt":"2020-05-20 08:00:00","desc":"\u5e0c\u671b\u4e0b\u4e00\u6b21\uff0c\u80fd\u559c\u6b22\u4e0a\u4e00\u4e2a\u4e5f\u559c\u6b22\u81ea\u5df1\u7684\u4eba \u200b\u200b\u200b\u200b\u3002","images":["http://gank.io/images/d237f507bf1946d2b0976e581f8aab9b"],"likeCounts":0,"publishedAt":"2020-05-20 08:00:00","stars":1,"title":"\u7b2c91\u671f","type":"Girl","url":"http://gank.io/images/d237f507bf1946d2b0976e581f8aab9b","views":2024},{"_id":"5e9591b6808d6d2fe6b56ed5","author":"\u9e22\u5a9b","category":"Girl","createdAt":"2020-05-19 08:00:00","desc":"\u8fd9\u4e2a\u4e16\u754c\u4e0a\uff0c\n\u6709\u4e9b\u4eba\u6709\u591a\u51b7\u6f20\uff0c\n\u6709\u4e9b\u4eba\u5c31\u6709\u591a\u6e29\u6696\uff0c\n\u5e0c\u671b\u4f60\u603b\u4f1a\u9047\u5230\u90a3\u4e9b\u6e29\u6696\u5bf9\u4f60\u7684\u4eba\u3002","images":["http://gank.io/images/25d3e3db2c1248bb917c09dc4f50a46f"],"likeCounts":1,"publishedAt":"2020-05-19 08:00:00","stars":1,"title":"\u7b2c90\u671f","type":"Girl","url":"http://gank.io/images/25d3e3db2c1248bb917c09dc4f50a46f","views":2843},{"_id":"5e9591a2ee6ba981da2af34a","author":"\u9e22\u5a9b","category":"Girl","createdAt":"2020-05-18 08:00:00","desc":"\u4ee5\u524d\u5bf9\u4f60\u7684\u559c\u6b22\uff0c\n\u662f\u89c1\u4f60\uff0c\u5ff5\u4f60\uff0c\u966a\u4f34\u4f60\u3002\n\u73b0\u5728\u5bf9\u4f60\u7684\u559c\u6b22\uff0c\n\u662f\u4e0d\u95ee\uff0c\u4e0d\u770b\uff0c\u4e0d\u53e8\u6270\u3002","images":["http://gank.io/images/19c99c447e0a40a6b3ff89951957cfb1"],"likeCounts":0,"publishedAt":"2020-05-18 08:00:00","stars":1,"title":"\u7b2c89\u671f","type":"Girl","url":"http://gank.io/images/19c99c447e0a40a6b3ff89951957cfb1","views":1926},{"_id":"5e959197808d6d2fe6b56ed4","author":"\u9e22\u5a9b","category":"Girl","createdAt":"2020-05-17 08:00:00","desc":"\u53ea\u8981\u7ed3\u5c40\u662f\u559c\u5267\uff0c\u8fc7\u7a0b\u4f60\u8981\u6211\u600e\u4e48\u54ed\u90fd\u884c\uff0c\u5e78\u798f\u53ef\u4ee5\u6765\u7684\u6162\u4e00\u4e9b\uff0c\n\u53ea\u8981\u5b83\u662f\u771f\u7684\uff0c\u5982\u679c\u6700\u540e\u80fd\u5728\u4e00\u8d77\uff0c\u665a\u70b9\u6211\u771f\u7684\u65e0\u6240\u8c13\u7684\u3002","images":["http://gank.io/images/f0c192e3e335400db8a709a07a891b2e"],"likeCounts":0,"publishedAt":"2020-05-17 08:00:00","stars":1,"title":"\u7b2c88\u671f","type":"Girl","url":"http://gank.io/images/f0c192e3e335400db8a709a07a891b2e","views":2746},{"_id":"5e95915f808d6d2fe6b56ed3","author":"\u9e22\u5a9b","category":"Girl","createdAt":"2020-05-16 08:00:00","desc":"\u82e5\u4e0d\u662f\u60c5\u6df1\u4f3c\u6d77\uff0c\u601d\u5ff5\u53c8\u600e\u4f1a\u6cdb\u6ee5\u6210\u707e\u3002","images":["http://gank.io/images/bdb35e4b3c0045c799cc7a494a3db3e0"],"likeCounts":3,"publishedAt":"2020-05-16 08:00:00","stars":1,"title":"\u7b2c87\u671f","type":"Girl","url":"http://gank.io/images/bdb35e4b3c0045c799cc7a494a3db3e0","views":3984}],"page":1,"page_count":10,"status":100,"total_counts":96}
利用这些数据可以生成一个实体Bean。
在app的com.llw.network下新建一个bean包,里面新建一个GankResponse类,里面的代码如下:
package com.llw.network.bean;
import java.util.List;
/**
* Gank返回数据
* @author llw
*/
public class GankResponse {
private int page;
private int page_count;
private int status;
private int total_counts;
private List<DataBean> data;
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPage_count() {
return page_count;
}
public void setPage_count(int page_count) {
this.page_count = page_count;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public int getTotal_counts() {
return total_counts;
}
public void setTotal_counts(int total_counts) {
this.total_counts = total_counts;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* _id : 5e959250808d6d2fe6b56eda
* author : 鸢媛
* category : Girl
* createdAt : 2020-05-25 08:00:00
* desc : 与其安慰自己平凡可贵,
不如拼尽全力活得漂亮。
* images : ["http://gank.io/images/f4f6d68bf30147e1bdd4ddbc6ad7c2a2"]
* likeCounts : 6
* publishedAt : 2020-05-25 08:00:00
* stars : 1
* title : 第96期
* type : Girl
* url : http://gank.io/images/f4f6d68bf30147e1bdd4ddbc6ad7c2a2
* views : 11898
*/
private String _id;
private String author;
private String category;
private String createdAt;
private String desc;
private int likeCounts;
private String publishedAt;
private int stars;
private String title;
private String type;
private String url;
private int views;
private List<String> images;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getLikeCounts() {
return likeCounts;
}
public void setLikeCounts(int likeCounts) {
this.likeCounts = likeCounts;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public int getStars() {
return stars;
}
public void setStars(int stars) {
this.stars = stars;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getViews() {
return views;
}
public void setViews(int views) {
this.views = views;
}
public List<String> getImages() {
return images;
}
public void setImages(List<String> images) {
this.images = images;
}
}
}
现在基本上就配置完毕了,下面就来简单使用一下,修改一个activity_main.xml
<?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:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
</LinearLayout>
里面也就一个图片控件而已,然后回到MainActivity中。
package com.llw.network;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.llw.network.api.ApiService;
import com.llw.network.bean.GankResponse;
import com.llw.network.observer.BaseObserver;
import java.util.List;
/**
* @author llw
*/
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
//访问网络
requestNetwork();
}
/**
* 访问网络
*/
@SuppressLint("CheckResult")
private void requestNetwork() {
NetworkApi.createService(ApiService.class)
.getList()
.compose(NetworkApi.applySchedulers(new BaseObserver<GankResponse>() {
/**
* 成功
* @param gankResponse
*/
@Override
public void onSuccess(GankResponse gankResponse) {
List<GankResponse.DataBean> data = gankResponse.getData();
if (data != null && data.size() > 0) {
String imgUrl = data.get(1).getImages().get(0);
Glide.with(MainActivity.this).load(imgUrl).into(imageView);
}else {
Toast.makeText(MainActivity.this, "数据为空", Toast.LENGTH_SHORT).show();
}
}
/**
* 失败
* @param e
*/
@Override
public void onFailure(Throwable e) {
KLog.e("MainActivity",e.toString());
Toast.makeText(MainActivity.this, "访问失败", Toast.LENGTH_SHORT).show();
}
}));
}
}
里面也就一个图片控件而已,然后回到MainActivity中。
package com.llw.network;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.llw.network.api.ApiService;
import com.llw.network.bean.GankResponse;
import com.llw.network.observer.BaseObserver;
import java.util.List;
/**
* @author llw
*/
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
//访问网络
requestNetwork();
}
/**
* 访问网络
*/
@SuppressLint("CheckResult")
private void requestNetwork() {
NetworkApi.createService(ApiService.class)
.getList()
.compose(NetworkApi.applySchedulers(new BaseObserver<GankResponse>() {
/**
* 成功
* @param gankResponse
*/
@Override
public void onSuccess(GankResponse gankResponse) {
List<GankResponse.DataBean> data = gankResponse.getData();
if (data != null && data.size() > 0) {
String imgUrl = data.get(1).getImages().get(0);
Glide.with(MainActivity.this).load(imgUrl).into(imageView);
}else {
Toast.makeText(MainActivity.this, "数据为空", Toast.LENGTH_SHORT).show();
}
}
/**
* 失败
* @param e
*/
@Override
public void onFailure(Throwable e) {
KLog.e("MainActivity",e.toString());
Toast.makeText(MainActivity.this, "访问失败", Toast.LENGTH_SHORT).show();
}
}));
}
}
然后运行一下。图片就加载出来了。
可以查看run里面的日志还记得这是在那里添加的吗,没错就是请求拦截器里面。也别忘了在返回拦截器中打印了请求时间。下面来看一下:
现在我们知道这个接口从请求到返回耗时459毫秒,而且通过这个自定义的日志打印工具类,你还能知道是在那里打印的日志,可以让你追根溯源。现在我们很明显是在debug模式下的,怎么证明呢?
还记得配置OkHttp这里吗?
那么BODY里面的信息也可以在Run下面找到,如下图所示:
现在是不是还差了一步呢?那就是设置网络了,还记得network模块中的NetworkEnvironmentActivity吗?该它出马了。
3. 切换网络环境
切换网络通常是采用特殊的方式,否则岂不是谁都知道了,还记得Android手机开启开发者模式这个方法吗?下面这个操作也是类似的。
可以点击这个图片多下然后进入到NetworkEnvironmentActivity中,现在进入MainActivity中,添加如下成员变量
//点击6次
private final int CLICK_NUM = 6;
//点击时间间隔5秒
private final int CLICK_INTERVAL_TIME = 5000;
//上一次的点击时间
private long lastClickTime = 0;
//记录点击次数
private int clickNum = 0;
添加连续点击的方法
/**
* 连续6次点击
*/
public void sixClick() {
//点击的间隔时间不能超过5秒
long currentClickTime = SystemClock.uptimeMillis();
if (currentClickTime - lastClickTime <= CLICK_INTERVAL_TIME || lastClickTime == 0) {
lastClickTime = currentClickTime;
clickNum = clickNum + 1;
} else {
//超过5秒的间隔
//重新计数 从1开始
clickNum = 1;
lastClickTime = 0;
return;
}
if (clickNum == CLICK_NUM) {
//重新计数
clickNum = 0;
lastClickTime = 0;
/*实现点击多次后的事件*/
Toast.makeText(MainActivity.this, "设置网络环境", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, NetworkEnvironmentActivity.class));
}
}
然后在onCreate中通过点击imageView调用这个sixClick()方法。
下面运行一下,这次我是在真机上运行的。
运行的效果就证明网络切换成功了,因为实际上我使用了两个完全不同访问地址,因此当切换到测试的地址之后,出现访问失败的提示,这个错误就是404。怎么证明了,还记得我在错误返回的时候打印的日志吗?OK,再切换到之前的网络看看。这样就可以了,那么这篇文章就到这里了。
九、源码
源码地址:NetworkFrameWorkDemo
总结
上述的内容,并不全是我自己的想法,网络上学到过一些,再加上平时开发中的积累,才写出来的,希望能对您有所帮助,当然网络访问框架通常并不是独立使用的,而是与框架组合起来使用,后续我可能会写,也可能不会。山高水长,后会有期~