引入
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'//Gson库
compile 'com.squareup.retrofit2:converter-gson:2.3.0'//Gson库
文件上传
接口方法定义:
@Multipart
@POST("/api/v1/face/add")
Call<FaceResponse> upLoadPicture(@PartMap Map<String, okhttp3. RequestBody> params,@Part MultipartBody.Part part);
@POST("/api/v1/face/add")
Call<FaceResponse> upLoadPicture(@PartMap Map<String, okhttp3. RequestBody> params,@Part MultipartBody.Part part);
RequestBody要使用okhttp3包下的,使用retrofit2下的时,传的参数值为空了。
params用于上传普通参数,part用于上传文件。
功能实现:
public static void addFace(String name, String imagefile, final HCBaseHttp.CallBack<com.cloudminds.hc.cloudService.bean.FaceResponse> callBack){
HCHttpEngine.startReq(new HCHttpFace(imagefile,name), CloudServiceContants.FACEHTTP,getCallback(callBack));
}
HCHttpEngine.startReq(new HCHttpFace(imagefile,name), CloudServiceContants.FACEHTTP,getCallback(callBack));
}
public static void startReq(HCBaseHttp req, String baseUrl, Callback callback){
Log.e(TAG, "startReq: " +req.toString()+" "+baseUrl);
Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
Call call = req.getCall(retrofit);
call.enqueue(callback);
Log.e(TAG, "startReq: enqueue" );
}
Log.e(TAG, "startReq: " +req.toString()+" "+baseUrl);
Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
Call call = req.getCall(retrofit);
call.enqueue(callback);
Log.e(TAG, "startReq: enqueue" );
}
private static <D> Callback<D> getCallback(final HCBaseHttp.CallBack<D> callBack) {
return new Callback<D>(){
return new Callback<D>(){
@Override
public void onResponse(Call<D> call, Response<D> response) {
Log.e(TAG, "onResponse: " +response.toString());
if(response.body()!=null) {
Log.e(TAG, "onResponse: " + response.body());
callBack.onResponse(response.body());
//
}else{
callBack.onFailure("response.body()=null");
}
}
public void onResponse(Call<D> call, Response<D> response) {
Log.e(TAG, "onResponse: " +response.toString());
if(response.body()!=null) {
Log.e(TAG, "onResponse: " + response.body());
callBack.onResponse(response.body());
//
}else{
callBack.onFailure("response.body()=null");
}
}
@Override
public void onFailure(Call<D> call, Throwable t) {
Log.e(TAG, "onFailure: "+t.getMessage() );
callBack.onFailure(t.getMessage());
}
};
}
public void onFailure(Call<D> call, Throwable t) {
Log.e(TAG, "onFailure: "+t.getMessage() );
callBack.onFailure(t.getMessage());
}
};
}
public abstract class HCBaseHttp {
abstract Call getCall(Retrofit retrofit);
public interface CallBack<S>{
void onResponse(S data);
void onFailure(String msg);
}
}
abstract Call getCall(Retrofit retrofit);
public interface CallBack<S>{
void onResponse(S data);
void onFailure(String msg);
}
}
public class HCHttpFace extends HCBaseHttp {
String imagefile;
String name;
private String TAG="HCApiClient";
String name;
private String TAG="HCApiClient";
public HCHttpFace(String imagefile, String name) {
this.imagefile = imagefile;
this.name = name;
}
this.imagefile = imagefile;
this.name = name;
}
public String getImagefile() {
return imagefile;
}
return imagefile;
}
public void setImagefile(String imagefile) {
this.imagefile = imagefile;
}
this.imagefile = imagefile;
}
public String getName() {
return name;
}
return name;
}
public void setName(String name) {
this.name = name;
}
this.name = name;
}
@Override
Call getCall(Retrofit retrofit) {
RequestService requestService = retrofit.create(RequestService.class);
String gson = new Gson().toJson(this);
Log.e(TAG, "getCall: "+gson );
Map<String, okhttp3.RequestBody> params = new HashMap<String, okhttp3.RequestBody>();
File file = new File(imagefile);
okhttp3.RequestBody fileRequestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("application/octet-stream"), file);
Call getCall(Retrofit retrofit) {
RequestService requestService = retrofit.create(RequestService.class);
String gson = new Gson().toJson(this);
Log.e(TAG, "getCall: "+gson );
Map<String, okhttp3.RequestBody> params = new HashMap<String, okhttp3.RequestBody>();
File file = new File(imagefile);
okhttp3.RequestBody fileRequestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("application/octet-stream"), file);
MultipartBody.Part partFile = MultipartBody.Part.createFormData("imagefile","aaa",fileRequestBody);
okhttp3.RequestBody o3requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"),CloudServiceContants.app_key);
params.put("app_key",o3requestBody);
o3requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"),CloudServiceContants.app_secret);
params.put("app_secret",o3requestBody);
o3requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"),CloudServiceContants.faceset_id);
params.put("faceset_id",o3requestBody);
o3requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"),name);
params.put("name",o3requestBody);
o3requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"),file.getName());
params.put("imagename",o3requestBody);
Call<FaceResponse> call = requestService.upLoadPicture(params,partFile);
return call;
}
okhttp3.RequestBody o3requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"),CloudServiceContants.app_key);
params.put("app_key",o3requestBody);
o3requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"),CloudServiceContants.app_secret);
params.put("app_secret",o3requestBody);
o3requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"),CloudServiceContants.faceset_id);
params.put("faceset_id",o3requestBody);
o3requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"),name);
params.put("name",o3requestBody);
o3requestBody = okhttp3.RequestBody.create(okhttp3.MediaType.parse("multipart/form-data"),file.getName());
params.put("imagename",o3requestBody);
Call<FaceResponse> call = requestService.upLoadPicture(params,partFile);
return call;
}
@Override
public String toString() {
return "HCHttpFace{" +
"imagefile='" + imagefile + '\'' +
", name='" + name + '\'' +
'}';
}
}
public String toString() {
return "HCHttpFace{" +
"imagefile='" + imagefile + '\'' +
", name='" + name + '\'' +
'}';
}
}