Android OkHttp+Retrofit+RxJava搭建网络访问框架(含源码)

本文介绍了如何在Android项目中搭建OkHttp、Retrofit和RxJava的网络访问框架,包括添加依赖、创建拦截器、自定义日志打印、配置网络访问信息和应用实例。提供了详细的步骤和代码示例,帮助开发者快速构建高效的网络请求模块。
摘要由CSDN通过智能技术生成

.onErrorResumeNext(new HttpErrorHandler());//判断有没有400的错误

//这里还少了对异常

//订阅观察者

observable.subscribe(observer);

return observable;

}

};

}

五、增加拦截器


拦截器中需要打印日志和时间转换,对此需要几个工具类,所以在com.llw.network下新建一个utils包,下面新建一个DateUitl

package com.llw.network.utils;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

import java.util.Locale;

public class DateUtil {

//获取当前完整的日期和时间

public static String getNowDateTime() {

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

return sdf.format(new Date());

}

//获取当前日期

public static String getNowDate() {

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);

return sdf.format(new Date());

}

//前一天

public static String getYesterday(Date date) {

String tomorrow = “”;

Calendar calendar = new GregorianCalendar();

calendar.setTime(date);

calendar.add(Calendar.DATE, -1);

date = calendar.getTime();

SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);

tomorrow = formatter.format(date);

return tomorrow;

}

//后一天

public static String getTomorrow(Date date) {

String tomorrow = “”;

Calendar calendar = new GregorianCalendar();

calendar.setTime(date);

calendar.add(Calendar.DATE, +1);

date = calendar.getTime();

SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);

tomorrow = formatter.format(date);

return tomorrow;

}

//获取当前时间

public static String getNowTime() {

SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm:ss”);

return sdf.format(new Date());

}

//获取当前日期(精确到毫秒)

public static String getNowTimeDetail() {

SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm:ss.SSS”);

return sdf.format(new Date());

}

//获取今天是星期几

public static String getWeekOfDate(Date date) {

String[] weekDays = {“星期日”, “星期一”, “星期二”, “星期三”, “星期四”, “星期五”, “星期六”};

Calendar cal = Calendar.getInstance();

cal.setTime(date);

int w = cal.get(Calendar.DAY_OF_WEEK) - 1;

if (w < 0) {

}

w = 0;

return weekDays[w];

}

//计算星期几

private static int getDayOfWeek(String dateTime) {

Calendar cal = Calendar.getInstance();

if (dateTime.equals(“”)) {

cal.setTime(new Date(System.currentTimeMillis()));

} else {

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”, Locale.getDefault());

Date date;

try {

date = sdf.parse(dateTime);

} catch (ParseException e) {

date = null;

e.printStackTrace();

}

if (date != null) {

cal.setTime(new Date(date.getTime()));

}

}

return cal.get(Calendar.DAY_OF_WEEK);

}

//根据年月日计算是星期几并与当前日期判断 非昨天、今天、明天 则以星期显示

public static String Week(String dateTime) {

String week = “”;

String yesterday = “”;

String today = “”;

String tomorrow = “”;

yesterday = getYesterday(new Date());

today = getNowDate();

tomorrow = getTomorrow(new Date());

if (dateTime.equals(yesterday)) {

week = “昨天”;

} else if (dateTime.equals(today)) {

week = “今天”;

} else if (dateTime.equals(tomorrow)) {

week = “明天”;

} else {

switch (getDayOfWeek(dateTime)) {

case 1:

week = “星期日”;

break;

case 2:

week = “星期一”;

break;

case 3:

week = “星期二”;

break;

case 4:

week = “星期三”;

break;

case 5:

week = “星期四”;

break;

case 6:

week = “星期五”;

break;

case 7:

week = “星期六”;

break;

}

}

return week;

}

//将时间戳转化为对应的时间(10位或者13位都可以)

public static String formatTime(long time) {

String times = null;

if (String.valueOf(time).length() > 10) {// 10位的秒级别的时间戳

times = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(new Date(time * 1000));

} else {// 13位的秒级别的时间戳

times = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(time);

}

return times;

}

//将时间字符串转为时间戳字符串

public static String getStringTimestamp(String time) {

String timestamp = null;

try {

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

Long longTime = sdf.parse(time).getTime() / 1000;

timestamp = Long.toString(longTime);

} catch (ParseException e) {

e.printStackTrace();

}

return timestamp;

}

}

同样再建一个KLog类,用于日志打印。

package com.llw.network.utils;

import android.text.TextUtils;

import android.util.Log;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

/**

  • 自定义日志类

*/

public final class KLog {

private static boolean IS_SHOW_LOG = true;

private static final String DEFAULT_MESSAGE = “execute”;

private static final String LINE_SEPARATOR = System.getProperty(“line.separator”);

private static final int JSON_INDENT = 4;

private static final int V = 0x1;

private static final int D = 0x2;

private static final int I = 0x3;

private static final int W = 0x4;

private static final int E = 0x5;

private static final int A = 0x6;

private static final int JSON = 0x7;

public static void init(boolean isShowLog) {

IS_SHOW_LOG = isShowLog;

}

public static void v() {

printLog(V, null, DEFAULT_MESSAGE);

}

public static void v(String msg) {

printLog(V, null, msg);

}

public static void v(String tag, String msg) {

printLog(V, tag, msg);

}

public static void d() {

printLog(D, null, DEFAULT_MESSAGE);

}

public static void d(String msg) {

printLog(D, null, msg);

}

public static void d(String tag, String msg) {

printLog(D, tag, msg);

}

public static void i() {

printLog(I, null, DEFAULT_MESSAGE);

}

public static void i(String msg) {

printLog(I, null, msg);

}

public static void i(String tag, String msg) {

printLog(I, tag, msg);

}

public static void w() {

printLog(W, null, DEFAULT_MESSAGE);

}

public static void w(String msg) {

printLog(W, null, msg);

}

public static void w(String tag, String msg) {

printLog(W, tag, msg);

}

public static void e() {

printLog(E, null, DEFAULT_MESSAGE);

}

public static void e(String msg) {

printLog(E, null, msg);

}

public static void e(String tag, String msg) {

printLog(E, tag, msg);

}

public static void a() {

printLog(A, null, DEFAULT_MESSAGE);

}

public static void a(String msg) {

printLog(A, null, msg);

}

public static void a(String tag, String msg) {

printLog(A, tag, msg);

}

public static void json(String jsonFormat) {

printLog(JSON, null, jsonFormat);

}

public static void json(String tag, String jsonFormat) {

printLog(JSON, tag, jsonFormat);

}

private static void printLog(int type, String tagStr, String msg) {

if (!IS_SHOW_LOG) {

return;

}

StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

int index = 4;

String className = stackTrace[index].getFileName();

String methodName = stackTrace[index].getMethodName();

int lineNumber = stackTrace[index].getLineNumber();

String tag = (tagStr == null ? className : tagStr);

methodName =

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值