retrofit of the notes content

 

problem?

6.retrofit principle: Dynamic Proxy mode
basic usage of 5.retrofit?
4. The use of retrofit projects (basic processes and file upload records and pictures)? Special: file (and pictures) to upload
the meaning and role of 3.Retrofit various annotations?
Methods 2.android retrofit request parameter format RequestBody of
1.retrofit model?

 

====  

5.retrofit basic usage:


public interface MovieService{
@GET("top250")
Call<MovieEntity> getTopMovie(@Query("start")int start,@Query("count") int count);
}
然后你还需要创建一个Retrofit对象:
public static final String baseUrl="https://api.douban.com/v2/movie/";
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();

The object is then to create a MovieService Retrofit objects:
MovieService movieService = retrofit.create (MovieService.class);
Call <MovieEntity> Call movieService.getTopMovie = (0,10);
//call.enqueue () asynchronous request, call.execute () synchronization request
call.enqueue (the Callback new new <MovieEntity> () {
@Override
public void onResponse (Call <MovieEntity> Call, the Response <MovieEntity> Response) {
result_TV.setText (response.body () toString ()).;
}
@Override
public void the onFailure (Call <MovieEntity> Call, the Throwable T) {
result_TV.setText (t.getMessage ());
}
});
here can also remove a request:
Retrofit not directly canceled version 1.x methodological reasons ongoing task in 2.x version, Service mode becomes the form of the Call is to make transactions in progress can be canceled.
To do this, simply call call.cancel ().
These are the basic usage Retrofit2.0 of the following detailed talk about its usage:


the use of retrofit 4. project (the basic process and file upload records and pictures)?

4.1项目中retrofit的使用流程:
public interface CommonApi {
@POST("common/getTime.htm")
Observable<BaseJson<Long>> getNetTime(@Body RequestBody requestBody);
}

public class UsualConfigModel extends BaseModel implements UsualConfigContract.Model {
@Override
public Observable<BaseJson<Long>> getNetTime(Map<String, Object> hashMap) {
Observable<BaseJson<Long>> observable = mRepositoryManager.obtainRetrofitService(CommonApi.class)
.getNetTime(ToRequestBodyUtil.toRequestBody(hashMap));
return observable;
}
}

public static RequestBody toRequestBody(Map<String, Object> map) {
String input = buildParams(map);
input = replaceURL(input);
//RequestBody requestBody = RequestBody.create(MediaType.parse("application/json, text/plain; charset=utf-8"), input);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"), input);
return requestBody;
}

public static RequestBody buildParamV2(Map<String, String> paramsMap, boolean isFilter) {
FormBody.Builder builder = new FormBody.Builder();
for (String key : paramsMap.keySet()) {
String value = paramsMap.get(key);
builder.add(key, value);// 追加表单信息
}
RequestBody formBody = builder.build();
//Request request = new Request.Builder().url(netUrl).post(formBody).build();
return formBody;
}

4.2 retrofit project files (and pictures) uploaded using the process:
VisitApi class:
/ **
* @des: Conference details - new: files files, upload pictures, etc.: ( "", param, files ) .then (());
// this manner:
* /
@POST ( "Record / Meeting / saveRecordMeetingInfo.htm")
Observable <MeetDetail> newAddMeetDetail (@Body requestBody body);

@Multipart
@POST ( "Record / Meeting / saveRecordMeetingInfo .htm ")
Observable <BaseCode> newAddMeetDetailV1 (@QueryMap the Map <String, Object> usermaps, @Part List <MultipartBody.Part> filemap,
@Part List <MultipartBody.Part> photoMap, @Part List <MultipartBody.Part> signMap) ;

@Multipart
@POST("record/meeting/saveRecordMeetingInfo.htm")
Observable<BaseCode> newAddMeetDetailV2(@QueryMap Map<String, Object> usermaps, @PartMap Map<String, RequestBody>fileMap,
@PartMap Map<String, RequestBody>photoMap, @PartMap Map<String, RequestBody> signMap);

/**
* @des: 会议修改的接口
*/
@Override
public Observable<MeetDetail> changedMeetDetail(Map<String, Object> hashMap, List<String> filePaths, List<String> photoPaths, List<String> signPaths) {
MultipartBody.Builder build = new MultipartBody.Builder();
for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
build.addFormDataPart(key, String.valueOf(value));//
}
if (filePaths != null && filePaths.size() > 0) {
for (int i = 0; i < filePaths.size(); i++) {
File file = new File(filePaths.get(i));
RequestBody fileRQ = RequestBody.create(MediaType.parse("multipart/form-data"), file);
build.addFormDataPart("files", file.getName(), fileRQ);//
}
}
if (photoPaths != null && photoPaths.size() > 0) {
for (int i = 0; i < photoPaths.size(); i++) {
File file = new File(photoPaths.get(i));
RequestBody fileRQ = RequestBody.create(MediaType.parse("image/*"), file);
build.addFormDataPart("photos", file.getName(), fileRQ);
}
}
MultipartBody body=build.build(); //MultipartBody继承RequestBody
Observable<MeetDetail> observable = mRepositoryManager.obtainRetrofitService(VisitApi.class)
.changedMeetDetail(body);
return observable;
}

// 不适合的方法:
public Observable<BaseCode> changedMeetDetail2(Map<String, Object> hashMap, List<String> filePaths, List<String> photoPaths, List<String> signPaths) {
List<MultipartBody.Part> parts1=new ArrayList<>();
if (filePaths != null && filePaths.size() > 0) {
for (int i = 0; i < filePaths.size(); i++) {
File file = new File(filePaths.get(i));
RequestBody fileRQ = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("files", file.getName(), fileRQ);
parts1.add(part);
}
}else {
MultipartBody.Part part = MultipartBody.Part.createFormData("","");// empty string directly into the two can not pass it ... null; }
parts1.add (Part);

List<MultipartBody.Part> parts2=new ArrayList<>();
if (photoPaths != null && photoPaths.size() > 0) {
for (int i = 0; i < photoPaths.size(); i++) {
File file = new File(photoPaths.get(i));
RequestBody fileRQ = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("photos", file.getName(), fileRQ);
parts2.add(part);
}
}else {
MultipartBody.Part part = MultipartBody.Part.createFormData("","");//直接传入两个空字符串就可以了...不能传null;
parts2.add(part);
}
Observable<BaseCode> observable = null;
// Observable<BaseCode> observable= mRepositoryManager.obtainRetrofitService(VisitApi.class)
// .changedMeetDetail(hashMap, parts1, parts2, parts3);
return observable;
}

Guess you like

Origin www.cnblogs.com/awkflf11/p/12541221.html