squareup 推出 retrofit2 已经有一段时间了,现在的版本比较稳定,没有什么大坑了。网络上的教程要么太简单,只是个Demo;要么有些落时,要么复用性比较差,所以自己写个教程,供大家参考。代码已上传至 https://github.com/Alex9Xu/RetrofitDemo

1. 首先在build.gradle引入依赖

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

注意,这里的 logging 用于输出网络交互的Log,对于开发调试极其有用。之前retrofit2因为不能输出Log被人嫌弃了很久,各高手实现了几种打印Log的方式,现在总算有官方的了。

2. 这是工具类

package com.alex9xu.hello.net;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable; import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory; import com.alex9xu.hello.config.AppConfigInterface;
import com.alex9xu.hello.utils.LogHelper; import java.io.IOException;
import java.lang.ref.WeakReference; /**
* Created by Alex9Xu@hotmail.com on 2016/7/13
*/
public class RetrofitBase {
private static final String TAG = "RetrofitBase"; private static Retrofit mRetrofit;
// private static Dialog mLoadingDialog;
private static WeakReference<Context> mContextRef; public static Retrofit retrofit() {
if (mRetrofit == null) {
OkHttpClient client; // Notice: The only differ of debug is: HttpLoggingInterceptor
// Common Params: "version" and "server_call_version" will in every request
if(!AppConfigInterface.isDebug) {
client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
HttpUrl originalHttpUrl = original.url();
HttpUrl url = originalHttpUrl.newBuilder()
.addQueryParameter("call_version", AppConfigInterface.TO_SERVER_VERSION)
.addQueryParameter("deviceType", AppConfigInterface.DEVICE_TYPE)
.build();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.addHeader("user-agent", "android")
.url(url);
Request request = requestBuilder.build();
return chain.proceed(request);
}
})
.build();
} else {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder()
.addInterceptor(logging)
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
HttpUrl originalHttpUrl = original.url();
HttpUrl url = originalHttpUrl.newBuilder()
.addQueryParameter("version", AppConfigInterface.TO_SERVER_VERSION)
.addQueryParameter("deviceType", AppConfigInterface.DEVICE_TYPE)
.build();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.addHeader("user-agent", "android")
.url(url);
Request request = requestBuilder.build();
return chain.proceed(request);
}
})
.build();
} mRetrofit = new Retrofit.Builder()
.baseUrl(AppConfigInterface.BASE_COM_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return mRetrofit;
} // Encapsulation Request and Response
public static <T> void AddToEnqueue(Call<T> baseCall, Context context, boolean isShowDlg,
final NetRequestListener listener) {
mContextRef = new WeakReference<>(context);
// if(isShowDlg && null == mLoadingDialog && null != mContextRef.get()) {
// mLoadingDialog = DialogUtil.showLoginDialog(mContextRef.get());
// mLoadingDialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
// }
// if(isShowDlg) {
// mLoadingDialog.show();
// } baseCall.enqueue(new Callback<T>() {
@Override
public void onResponse(Call<T> call, retrofit2.Response<T> response) {
LogHelper.d(TAG, "toEnqueue, onResponse:");
if (null != response.body()) {
if(response.code() == 200) {
LogHelper.d(TAG, "toEnqueue, onResponse Suc");
// if(null != mLoadingDialog) {
// mLoadingDialog.dismiss();
// }
listener.onRequestSuc(response.code(), response);
} else {
LogHelper.d(TAG, "toEnqueue, onResponse Fail:" + response.code());
// if(null != mLoadingDialog) {
// mLoadingDialog.dismiss();
// }
listener.onRequestFail(response.code(), response.message());
}
} else {
LogHelper.d(TAG, "toEnqueue, onResponse Fail");
// if(null != mLoadingDialog) {
// mLoadingDialog.dismiss();
// }
listener.onRequestFail(response.code(), response.message());
}
} @Override
public void onFailure(Call<T> call, Throwable t) {
LogHelper.d(TAG, "toEnqueue, onFailure Fail");
// if(null != mLoadingDialog) {
// mLoadingDialog.dismiss();
// }
listener.onRequestFail(AppConfigInterface.RESULT_FAIL_UNKNOW, null);
}
});
} public static void stopLoadingDlg(Context context) {
// if(null != mContextRef && context == mContextRef.get() && null != mLoadingDialog) {
// mLoadingDialog.dismiss();
// mLoadingDialog = null;
// }
} }

讲解一下:

(1) 通过 addInterceptor 实现的打印日志及加入多个公共参数功能。

(2) 除了含有 HttpLoggingInterceptor 外,测试的和正式的,没有任何区别。通过全局变量控制是否为正式环境,如果是正式环境则不输出网络交互相关的Log。

(3) 可以通过 addQueryParameter("deviceType", "0") 的形式加入多个公共参数,这样所有的请求都会带该参数。

(4) 这里 BASE_COM_URL 是 http://test.hello.com/ 的形式。

3. 使用方式:

(1) 先写接口

package com.alex9xu.hello.net.apis;

import android.support.v4.util.ArrayMap;
import com.alex9xu.hello.config.AppConfigInterface;
import com.alex9xu.hello.model.WeatherResult; import retrofit2.Call; import retrofit2.http.GET;
import retrofit2.http.QueryMap; /**
* Created by Alex9Xu@hotmail.com on 2016/7/14
*/ public interface CityWeatherApi {
@GET(AppConfigInterface.GET_WEATHER)
Call<WeatherResult> getClassify(@QueryMap ArrayMap<String,String> paramMap);
}

这里通过Get提交参数,参数存储在Map里,可以添加多组参数。注意,我使用了ArrayMap,这是Android里特有的一种形式,内存占用只有HashMap的十分之一左右。

String CLASSIFYLIST = "query/classify.html";

(2) 再写返回值结构

package com.alex9xu.hello.model.Entity;

/**
* Created by Alex9Xu@hotmail.com on 2016/7/18
*/
public class Weatherinfo {
private String city;
private String cityid;
private String temp;
private String WD;
private String WS;
private String SD;
private String WSE;
private String time;
private String isRadar;
private String Radar;
private String njd;
private String qy; public String getCity() {
return city;
} public String getTemp() {
return temp;
}
}
package com.alex9xu.hello.model;

import com.alex9xu.hello.model.Entity.Weatherinfo;

/**
* Created by Alex9Xu@hotmail.com on 2016/7/18
*/
public class WeatherResult { private Weatherinfo weatherinfo; public Weatherinfo getWeatherinfo() {
return weatherinfo;
} }

返回的数据写成如上形式,以利于复用。

(3) 调用

import com.alex9xu.test.model.ClassifyListResult;
import com.alex9xu.test.model.entity.ClassfiyBean;
import com.alex9xu.test.net.ClassifyApi;
import com.alex9xu.test.net.RetrofitBase;
/** * Created by Alex9Xu@hotmail.com on 2016/7/14
*/
public class MainActivity extends BaseActivity {
// Note: make all the Activities extends BaseActivity to manage
private static final String TAG = "MainActivity"; private TextView mTvwShowInfo;
private Call<WeatherResult> mWeatherCall;
  ...
private void getData() {
// Notice: ArrayMap requires less memory in Android compare with HashMap (about 10%)
CityWeatherApi classifyApi = RetrofitBase.retrofit().create(CityWeatherApi.class);
ArrayMap<String,String> paramMap = new ArrayMap<>();
paramMap.put("sortType", "1");
paramMap.put("uid", "654321");
mWeatherCall = classifyApi.getClassify(paramMap); RetrofitBase.AddToEnqueue(mWeatherCall, MainActivity.this, true, new NetRequestListener() {
@Override
public void onRequestSuc(int code, Response response) {
LogHelper.d(TAG, "onRequestSuc");
Response<WeatherResult> resultResponse = response;
if(null != resultResponse.body().getWeatherinfo()) {
Weatherinfo info = resultResponse.body().getWeatherinfo();
StringBuilder strBld = new StringBuilder();
strBld.append(info.getCity());
strBld.append(getString(R.string.temperature));
strBld.append(getString(R.string.colon));
strBld.append(info.getTemp());
mTvwShowInfo.setText(strBld.toString());
}
} @Override
public void onRequestFail(int code, String reason) {
LogHelper.d(TAG, "onRequestFail: " + code + ", " + reason);
}
});
} @Override
protected void onStop() {
super.onStop();
// Notice: If the web operate is to update UI, you can cancel it when onStop
mWeatherCall.cancel();
}
...

讲解:会拼接成 https://test.hello.com/query/classify.html?uid=654321&version=1.0&Id=123456&deviceType=0 ,注意,其中两项是公共参数。

好了,这样就可以正常运行了。

retrofit2 使用教程 及 Android 网络架构搭建 (原创)的更多相关文章

  1. Retrofit2.0+RxJava+Dragger2实现不一样的Android网络架构搭建

    Tamic :csdn http://blog.csdn.net/sk719887916 众所周知,手机APP的核心就在于调用后台接口,展示相关信息,方便我们在手机上就能和外界交互.所以APP中网络框 ...

  2. 基于Retrofit2.0+RxJava+Dragger2实现不一样的Android网络构架搭建(转载)

    转载请注明出处:http://blog.csdn.net/finddreams/article/details/50849385#0-qzone-1-61707-d020d2d2a4e8d1a374a ...

  3. android 学习运用海马模拟器教程与android环境的搭建

    第三方海马玩模拟器 第一天的学习android采用的模拟器是海马,因此就分享给大家海马模拟器的相关步骤: 海马玩模拟器官网: http://droid4x.haimawan.com 下载相关平台的模拟 ...

  4. Android网络编程(一)HTTP协议原理

    相关文章 Android网络编程(一)HTTP协议原理 Android网络编程(二)HttpClient与HttpURLConnection Android网络编程(三)Volley使用方法全解析 A ...

  5. Android Navigation 架构组件入门教程

    Android Navigation 架构组件入门教程 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:https://www.cnblogs.com/cavalier-/p/1 ...

  6. Android 网络教程: 开始

    原文:Android Networking Tutorial: Getting Started 作者:Eunice Obugyei 译者:kmyhy 从 API 级别 1 开始,网络始终是 Andro ...

  7. 转---详细的Android开发环境搭建教程

    五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程 引言   在windows安装Android的开发环境不简单也说不上算复杂,本文写给第一次想在自己Windows上建立A ...

  8. Android基础新手教程——1.2 开发环境搭建

    Android基础新手教程--1.2 开发环境搭建 标签: Android基础新手教程 如今主流的Android开发环境有: ①Eclipse + ADT + SDK ②Android Studio ...

  9. Android开发环境搭建篇详尽的教程实例汇

    原文链接:http://android.eoe.cn/topic/android_sdk 一.android开发环境搭建图文教程整理篇: 1.Android开发环境搭建全程演示(jdk+eclip+a ...

随机推荐

  1. 计算广义积分$$\int_0^{+\infty}\cos x^p {\rm d}x,\int_0^{+\infty}\sin x^p {\rm d}x, p>1$$

    ${\bf 解:}$ 在角状域$G=\{z\in\mathbb{C}|0<{\rm Arg}z<\frac{\pi}{2p}\}$上引入辅助函数$e^{iz^p}$, 其中$z^p=|z| ...

  2. BZOJ 1020 安全的航线flight

    Description 在设计航线的时候,安全是一个很重要的问题.首先,最重要的是应采取一切措施确保飞行不会发生任何事故,但同时也需要做好最坏的打算,一旦事故发生,就要确保乘客有尽量高的生还几率.当飞 ...

  3. Replicate String in C#

    My original posting on string repetition caused a couple responses, and is currently among the Top P ...

  4. sicp第1章

    牛顿迭代法求平方: (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) ...

  5. zabbix 参数说明

    <pre name="code" class="html">数据采集的工作模式可以分为被动模式(服务器端到客户端采集数据) 主动模式(客户端主动上报 ...

  6. 分页SQL模板

    select * from ( select rownum as rn ,a.* from ( select * from page a where object_id >1000 and ow ...

  7. Linux Shell编程(6)——变量替换

    变量的名字是它的值保存的地方.引用它的值称为变量替换.$让我们仔细地区别变量和变量的值.如果variable1是一个变量的名字,那么$variable1就是引用这个变量的值――即这个变量它包含的数据. ...

  8. sed文本处理--文本行扩展与分割

    原文件: 00 abc sdfdsf1 2 45 3 400 xfd xdfssdf3 322 345 234 处理后文件: 00 abc sdfdsf 1 2 400 abc sdfdsf 5 3 ...

  9. 【动态规划】【最长公共子序列】Vijos P1111 小胖的水果

    题目链接: https://vijos.org/p/1111 题目大意: 多组数据,给两个字符串s1,s2,求把s1,s2拆开从前往后合并后最短是多少 apple + peach = appleach ...

  10. c++转换构造函数和类型转换函数

    看stl源码时,有一段代码感觉很奇怪 iterator begin() { return (link_type)((*node).next); } iterator和link_type是两种不同类型, ...