<p>对于Android中的联网请求, 使用Retrofit能够统一 定义接口, 并对相同的操作进行封装</p><h2>添加所需的依赖和权限:</h2><div>新建一个项目,在根目录的build.gradle的dependencies节点中添加,用于注解<br /></div><div><span style="color: rgb(51, 51, 51); font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.8px; white-space: pre; background-color: rgb(248, 248, 248);">classpath </span><span class="hljs-string" style="box-sizing: border-box; outline: none; color: rgb(221, 17, 68); font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.8px; white-space: pre;">'com.neenbedankt.gradle.plugins:android-apt:1.8'</span><br /></div><div><span class="hljs-string" style="box-sizing: border-box; outline: none; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.8px; white-space: pre; background-color: rgb(248, 248, 248);"><span style="color:#333333;">如我的项目根目录:</span></span></div><div><span class="hljs-string" style="box-sizing: border-box; outline: none; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.8px; white-space: pre; background-color: rgb(248, 248, 248);"><span style="color:#333333;"></span></span><pre name="code" class="html">// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
//用于注解
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}</pre><br /></div><div>主程序app module中build.gradle的第二行添加,用于注解</div><div>apply plugin: 'com.neenbedankt.android-apt'<br /></div><div><br /></div><div>dependencies节点中添加:</div><div>compile 'com.android.support:appcompat-v7:24.2.1'<br />compile 'com.android.support:design:24.2.1'<br />//布局注解<br />apt 'com.jakewharton:butterknife-compiler:8.0.1'<br />compile 'com.jakewharton:butterknife:8.0.1'<br />//响应式编程<br />compile 'io.reactivex:rxandroid:1.1.0'<br />compile 'io.reactivex:rxjava:1.1.0'<br />//联网类库<br />compile 'com.squareup.retrofit2:retrofit:2.1.0'<br />compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'<br />compile 'com.squareup.retrofit2:converter-scalars:2.1.0'<br />compile 'com.dou361.retrofit2:jjdxm-retrofit-converter-fastjson:1.0.0'<br />compile 'com.squareup.okhttp3:okhttp:3.3.0'<br /></div><div><br /></div><div>注意:这里使用的是fastJson用于处理返回的json数据, 别忘了在清单文件中 添加 联网权限</div><div><br /></div><div><br /></div><div><h2>封装网络请求类</h2></div><div>1 网络接口请求服务类</div><div> <pre name="code" class="java">public interface IApiService {
/** 查询历史的今天 */
@GET("/japi/toh")
Observable<RepoHistory> searchHistory(@QueryMap Map<String, String> map);
/** 加载笑话列表 */
@GET("/joke/content/list.from")
Observable<RepoJoke> loadJoke(@QueryMap Map<String, String> map);
}</pre>2 网络请求接口基类</div><div><pre name="code" class="java">package com.example.mythinkmvp.api;
import com.dou361.retrofit2.converter.fastjson.FastJsonConverterFactory;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
/**
* Created by zmybi on 2017/2/13.
* 网络请求接口基类
* 传入参数 可以得到一个接口
* 封装好连接超时,读取超时
*/
public class BaseApi {
public static IApiService getIApiService(String ip) {
return getService(ip);
}
public static IApiService getService(String ip) {
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30,TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ip)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(FastJsonConverterFactory.create())
.build();
return retrofit.create(IApiService.class);
}
}
</pre>3 网络请求接口的工具类</div><div><pre name="code" class="java">package com.example.mythinkmvp.api;
import com.example.mythinkmvp.bean.Joke;
import com.example.mythinkmvp.bean.RepoHistory;
import java.util.HashMap;
import java.util.Map;
import rx.Observable;
/**
* Created by zmybi on 2017/2/13.
*
* 网络接口请求的工具类
*/
public class ApiUtils extends BaseApi{
public static final String BASE_HISTORY = "http://api.juheapi.com/"; //历史记录的基本APi接口
public static final String BASE_JOK = "http://japi.juhe.cn/"; //历史记录的基本APi接口
public static Observable<RepoHistory> getPoHistory(String month,String day) {
IApiService iApiService = getIApiService(BASE_HISTORY);
//将get请求后面的参数 进行封装
Map<String,String> map = new HashMap<>();
map.put("key", "7ac7e02ff7f1f8f1ccdc2f9e5dddb6be");
map.put("v", "1.0");
map.put("month", month);
map.put("day", day);
Observable<RepoHistory> history = iApiService.getRepoHistory(map);
return history;
}
public static Observable<Joke> getJok(String page) {
/**key=d796a03545bddee0b56d913111f5f199&page=2&pagesize=10&sort=asc&time=1418745237*/
Map<String, String> map = new HashMap<>();
map.put("key", "d796a03545bddee0b56d913111f5f199");
map.put("sort", "asc");
map.put("time", "1418745237");
map.put("page", page);
map.put("pagesize", "10");
return getIApiService(BASE_JOK).getJok(map);
}
}
</pre><br />则在开发中直接定义api包, 将三个类,接口文件放入即可,直接在网络请求的工具类中定义接口 即可;<br /><br /></div><div><span class="hljs-string" style="box-sizing: border-box; outline: none; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.8px; white-space: pre; background-color: rgb(248, 248, 248);"><span style="color:#333333;"><br /></span></span></div>
buildscript {
repositories {
jcenter()
}
dependencies {
//用于注解
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}</pre><br /></div><div>主程序app module中build.gradle的第二行添加,用于注解</div><div>apply plugin: 'com.neenbedankt.android-apt'<br /></div><div><br /></div><div>dependencies节点中添加:</div><div>compile 'com.android.support:appcompat-v7:24.2.1'<br />compile 'com.android.support:design:24.2.1'<br />//布局注解<br />apt 'com.jakewharton:butterknife-compiler:8.0.1'<br />compile 'com.jakewharton:butterknife:8.0.1'<br />//响应式编程<br />compile 'io.reactivex:rxandroid:1.1.0'<br />compile 'io.reactivex:rxjava:1.1.0'<br />//联网类库<br />compile 'com.squareup.retrofit2:retrofit:2.1.0'<br />compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'<br />compile 'com.squareup.retrofit2:converter-scalars:2.1.0'<br />compile 'com.dou361.retrofit2:jjdxm-retrofit-converter-fastjson:1.0.0'<br />compile 'com.squareup.okhttp3:okhttp:3.3.0'<br /></div><div><br /></div><div>注意:这里使用的是fastJson用于处理返回的json数据, 别忘了在清单文件中 添加 联网权限</div><div><br /></div><div><br /></div><div><h2>封装网络请求类</h2></div><div>1 网络接口请求服务类</div><div> <pre name="code" class="java">public interface IApiService {
/** 查询历史的今天 */
@GET("/japi/toh")
Observable<RepoHistory> searchHistory(@QueryMap Map<String, String> map);
/** 加载笑话列表 */
@GET("/joke/content/list.from")
Observable<RepoJoke> loadJoke(@QueryMap Map<String, String> map);
}</pre>2 网络请求接口基类</div><div><pre name="code" class="java">package com.example.mythinkmvp.api;
import com.dou361.retrofit2.converter.fastjson.FastJsonConverterFactory;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
/**
* Created by zmybi on 2017/2/13.
* 网络请求接口基类
* 传入参数 可以得到一个接口
* 封装好连接超时,读取超时
*/
public class BaseApi {
public static IApiService getIApiService(String ip) {
return getService(ip);
}
public static IApiService getService(String ip) {
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30,TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ip)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(FastJsonConverterFactory.create())
.build();
return retrofit.create(IApiService.class);
}
}
</pre>3 网络请求接口的工具类</div><div><pre name="code" class="java">package com.example.mythinkmvp.api;
import com.example.mythinkmvp.bean.Joke;
import com.example.mythinkmvp.bean.RepoHistory;
import java.util.HashMap;
import java.util.Map;
import rx.Observable;
/**
* Created by zmybi on 2017/2/13.
*
* 网络接口请求的工具类
*/
public class ApiUtils extends BaseApi{
public static final String BASE_HISTORY = "http://api.juheapi.com/"; //历史记录的基本APi接口
public static final String BASE_JOK = "http://japi.juhe.cn/"; //历史记录的基本APi接口
public static Observable<RepoHistory> getPoHistory(String month,String day) {
IApiService iApiService = getIApiService(BASE_HISTORY);
//将get请求后面的参数 进行封装
Map<String,String> map = new HashMap<>();
map.put("key", "7ac7e02ff7f1f8f1ccdc2f9e5dddb6be");
map.put("v", "1.0");
map.put("month", month);
map.put("day", day);
Observable<RepoHistory> history = iApiService.getRepoHistory(map);
return history;
}
public static Observable<Joke> getJok(String page) {
/**key=d796a03545bddee0b56d913111f5f199&page=2&pagesize=10&sort=asc&time=1418745237*/
Map<String, String> map = new HashMap<>();
map.put("key", "d796a03545bddee0b56d913111f5f199");
map.put("sort", "asc");
map.put("time", "1418745237");
map.put("page", page);
map.put("pagesize", "10");
return getIApiService(BASE_JOK).getJok(map);
}
}
</pre><br />则在开发中直接定义api包, 将三个类,接口文件放入即可,直接在网络请求的工具类中定义接口 即可;<br /><br /></div><div><span class="hljs-string" style="box-sizing: border-box; outline: none; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.8px; white-space: pre; background-color: rgb(248, 248, 248);"><span style="color:#333333;"><br /></span></span></div>