Square开发的retrofit——类型安全的REST安卓客户端请求库
http://square.github.io/retrofit/
http://github.com/square/retrofit
https://github.com/square/retrofit/tree/...
Square 公司开发的 OkHttp——专注于性能和易用性的 HTTP 客户端
https://github.com/square/okhttp
https://github.com/square/okhttp/tree/ma...
facebook 出品的stetho——Android开发调试(查看网络请求和本地数据,包含本地数据库)
dependencies {
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
compile 'com.facebook.stetho:stetho:1.3.1'
compile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
}
获取列表数据【GET】
Map<String, String> map = new HashMap<String, String>();
map.put("userId" , userId);
map.put("page" , page);
map.put("pageSize" , pageSize);
//cmd:user/follow/list
//Follow:实体类
RetrofitApi.getInstance().getList(Follow[].class, cmd, map, listener);
获取单个实体数据【GET】
Map<String, String> map = new HashMap<String, String>();
map.put("userId" , userId);
map.put("followId" , followId);
//cmd:user/follow/detail
//Follow:实体类
RetrofitApi.getInstance().getModel(Follow.class, cmd, map, listener);
提交数据【POST】
Map<String, String> map = new HashMap<String, String>();
map.put("userId" , userId);
map.put("followId" , followId);
//cmd:user/follow/add
RetrofitApi.getInstance().postData(cmd, map, listener);
上传头像【POST】
File file = new File(filePath);
Map<String, RequestBody> map = new HashMap<String, RequestBody>();
map.put("userId", RetrofitApi.getInstance().parseRequestBody(userId));
map.put(RetrofitApi.getInstance().parseImageMapKey("avatar", file.getName()), RetrofitApi.getInstance().parseImageRequestBody(file));
//cmd:user/avatar/upload
RetrofitApi.getInstance().uploadFile(cmd, map, listener);
public interface RetrofitService {
@GET ("{url}")
Call<JsonObject> getData(
@Path("url") String url,
@QueryMap Map<String, String> map
);
@FormUrlEncoded
@POST( "{url}")
Call<JsonObject> postData(
@Path("url") String url,
@FieldMap Map<String, String> map
);
@Multipart
@POST( "{url}")
Call<JsonObject> uploadFile(
@Path("url") String url,
@PartMap Map<String, RequestBody> map
);
}
public interface CallbackListener {
void onSuccess(Object data, String msg);
void onFail(String msg);
}
package com.su.xier.retrofit;
import com.su.xier.BuildConfig;
import com.su.xier.okhttp.OkHttpHandle;
import com.su.xier.util.AppUtil;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.socks.library.KLog;
import java.io.File;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.PartMap;
import retrofit2.http.Path;
import retrofit2.http.QueryMap;
/**
* Created by xier on 2016/3/31.
*/
public class RetrofitApi<T > {
public static int TYPE_NORMAL = 0 ;
public static int TYPE_LIST = 1;
public static int TYPE_MODEL = 2 ;
public static int TYPE_POST = 3;
public static OkHttpClient okHttpClient;
static {
OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
okHttpBuilder.addNetworkInterceptor(new StethoInterceptor());
// okHttpBuilder.addInterceptor(new HttpLoggingInterceptor());
okHttpBuilder.addInterceptor( new HttpLoggingInterceptor( new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
KLog.i("OkHttp", message);
}
}).setLevel(HttpLoggingInterceptor.Level.BODY));//网络和日志拦截
}
okHttpBuilder.connectTimeout(OkHttpHandle.TIME_OUT, TimeUnit.SECONDS).readTimeout(OkHttpHandle.TIME_OUT, TimeUnit.SECONDS).writeTimeout(OkHttpHandle.TIME_OUT, TimeUnit.SECONDS);//设置请求超时
okHttpClient = okHttpBuilder.build();
}
private static final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://com.base.url/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
private static RetrofitApi retrofitApi;
public static RetrofitApi getInstance() {
if (null == retrofitApi) return new RetrofitApi();
return retrofitApi;
}
public void getNormal(String url, Map<String, String> map, final OkHttpHandle.CallbackListener listener) {
callEnqueue(TYPE_NORMAL, null, url, map, listener);
}
public void getList(final Class<T> tClass, String url, Map<String, String> map, final OkHttpHandle.CallbackListener listener) {
callEnqueue(TYPE_LIST, tClass, url, map, listener);
}
public void getModel(final Class<T> tClass, String url, Map<String, String> map, final OkHttpHandle.CallbackListener listener) {
callEnqueue(TYPE_MODEL, tClass, url, map, listener);
}
public void postData(String url, Map<String, String> map, final OkHttpHandle.CallbackListener listener) {
callEnqueue(TYPE_POST , null, url, map, listener);
}
public void callEnqueue(final int type, final Class< T> tClass, String url, Map<String, String> map, final OkHttpHandle.CallbackListener listener) {
Call<JsonObject> call;
if (type == TYPE_POST) {//POST
call = ((RetrofitService) retrofit.create(RetrofitService.class)).postData(url, map);
} else {//GET
call = ((RetrofitService) retrofit.create(RetrofitService.class)).getData(url, map);
}
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.code() == 200) {
try {
JsonObject json = response.body();
if (json.get( "status").getAsInt() >= 0 ) {
if ( type == TYPE_LIST) { //列表
JsonObject result = json.getAsJsonObject("result");
JsonArray jsonArray = result.getAsJsonArray("list" );
listener.onSuccess(Arrays. asList(new GsonBuilder().create().fromJson(jsonArray, (Class< T[]>) tClass)), result.get("total" ).getAsString());
} else if ( type == TYPE_MODEL) { //单个对象
JsonObject result = json.getAsJsonObject("result");
listener.onSuccess( new GsonBuilder().create().fromJson(result, tClass), json.get("message" ).getAsString());
} else { //普通JsonObject
if (json.has( "result") && json.get("result" ).isJsonObject()) {
listener.onSuccess(json.getAsJsonObject("result"), json.get("message" ).getAsString());
} else {
listener.onSuccess( null, json.get("message" ).getAsString());
}
}
} else {
listener.onFail(json.get( "message").getAsString());
}
} catch (Exception e) {
listener.onFail(AppUtil. getRequestException());
}
} else {
listener.onFail(response.message());
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
listener.onFail(AppUtil.getRequestException());
}
});
}
public void uploadFile(String url, Map<String, RequestBody> map, final OkHttpHandle.CallbackListener listener) {
Call<JsonObject> call = ((RetrofitService) retrofit.create(RetrofitService.class)).uploadFile(url, map);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.code() == 200) {
try {
JsonObject json = response.body();
if (json.get( "status").getAsInt() >= 0 ) {
if (json.has( "result") && json.get("result" ).isJsonObject()) {//单张图片 -返回图片路径
// listener.onSuccess(json.getAsJsonObject("result"), json.get("message").getAsString());
JsonObject result = json.getAsJsonObject("result" );
if (result.has( "url")) {
listener.onSuccess(result.get("url").getAsString(), json.get("message" ).getAsString());
} else {
listener.onSuccess( null, json.get("message" ).getAsString());
}
} else {
listener.onSuccess( null, json.get("message" ).getAsString());
}
} else {
listener.onFail(json.get( "message").getAsString());
}
} catch (Exception e) {
listener.onFail(AppUtil. getRequestException());
}
} else {
listener.onFail(response.message());
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
listener.onFail(AppUtil.getRequestException());
}
});
}
public RequestBody parseRequestBody(String value) {
return RequestBody.create(MediaType. parse("text/plain"), value);
}
public RequestBody parseImageRequestBody(File file) {
return RequestBody.create(MediaType. parse("image/jpg"), file);
}
public String parseImageMapKey(String key, String fileName) {
return key + "\"; filename=\"" + fileName;
}
}