资源预览内容
第1页 / 共12页
第2页 / 共12页
第3页 / 共12页
第4页 / 共12页
第5页 / 共12页
第6页 / 共12页
第7页 / 共12页
第8页 / 共12页
第9页 / 共12页
第10页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
RetrofitRetrofit 和 Java 领域的 ORM 概念类似, ORM 把结构化数据转换为 Java 对象,而Retrofit 把 REST API 返回的数据转化为 Java 对象方便操作。同时还封装了网络代码的调用。例如:public interface GitHubService GET(“/users/user/repos“)List listRepos(Path(“user“) String user);定义上面的一个 REST API 接口。 该接口定义了一个函数 listRepos , 该函数会通过HTTP GET 请求去访问服务器的/users/user/repos 路径并把返回的结果封装为List Java 对象返回。其中 URL 路径中的user的值为 listRepos 函数中的参数 user 的取值。然后通过 RestAdapter 类来生成一个 GitHubService 接口的实现;GitHubService service = restAdapter.create(GitHubService.class);获取接口的实现后就可以调用接口函数来和服务器交互了;List repos = service.listRepos(“octocat“);从上面的示例可以看出, Retrofit 使用注解来声明 HTTP 请求1.支持 URL 参数替换和查询参数2.返回结果转换为 Java 对象(返回结果可以为 JSON, protocol buffers)3.支持 Multipart 请求和文件上传具体使用文档具体使用文档函数和函数参数上的注解声明了请求方式请求方式请求方式每个函数都必须带有 HTTP 注解来表明请求方式和请求的 URL 路径。类库中有 5 个 HTTP注解: GET , POST , PUT , DELETE , 和 HEAD。 注解中的参数为请求的相对 URL 路径。GET(“/users/list“)在 URL 路径中也可以指定 URL 参数GET(“/users/list?sort=desc“)URL 处理处理请求的 URL 可以根据函数参数动态更新。一个可替换的区块为用 和 包围的字符串,而函数参数必需用 Path 注解表明,并且注解的参数为同样的字符串GET(“/group/id/users“) /注意 字符串idList groupList(Path(“id“) int groupId); /注意 Path注解的参数要和前面的字符串一样 id还支持查询参数GET(“/group/id/users“)List groupList(Path(“id“) int groupId, Query(“sort“) String sort);请求体(请求体(Request Body)通过 Body 注解可以声明一个对象作为请求体发送到服务器。POST(“/users/new“)void createUser(Body User user, Callback cb);对象将被 RestAdapter 使用对应的转换器转换为字符串或者字节流提交到服务器。FORM ENCODED AND MULTIPART 表单和表单和 Multipart函数也可以注解为发送表单数据和 multipart 数据使用 FormUrlEncoded 注解来发送表单数据;使用 Field 注解和参数来指定每个表单项的 Key,value 为参数的值。FormUrlEncodedPOST(“/user/edit“)User updateUser(Field(“first_name“) String first, Field(“last_name“) String last);使用 Multipart 注解来发送 multipart 数据。使用 Part 注解定义要发送的每个文件。MultipartPUT(“/user/photo“)User updateUser(Part(“photo“) TypedFile photo, Part(“description“) TypedString description);Multipart 中的 Part 使用 RestAdapter 的转换器来转换,也可以实现 TypedOutput来自己处理序列化。异步异步 VS 同步同步每个函数可以定义为异步或者同步。具有返回值的函数为同步执行的。GET(“/user/id/photo“)Photo listUsers(Path(“id“) int id);而异步执行函数没有返回值并且要求函数最后一个参数为 Callback 对象GET(“/user/id/photo“)void listUsers(Path(“id“) int id, Callback cb);在 Android 上,callback 对象会在主(UI)线程中调用。而在普通 Java 应用中,callback 在请求执行的线程中调用。服务器结果转换为 Java 对象使用 RestAdapter 的转换器把 HTTP 请求结果(默认为 JSON)转换为 Java 对象,Java对象通过函数返回值或者 Callback 接口定义GET(“/users/list“)List userList();GET(“/users/list“)void userList(Callback cb);如果要直接获取 HTTP 返回的对象,使用 Response 对象。GET(“/users/list“)Response userList();GET(“/users/list“)void userList(Callback cb);项目主页: http:/square.github.io/retrofit/参考项目: http:/square.github.io/okhttp/Android 示例项目: https:/github.com/goodev/RetrofitDemoRetrofit 在项目中的应用在项目中的应用定义一个注解,用来对 bean 进行要序列化的字段java view plaincopy1.import java.lang.annotation.Documented; 2.import java.lang.annotation.ElementType; 3.import java.lang.annotation.Retention; 4.import java.lang.annotation.RetentionPolicy; 5.import java.lang.annotation.Target; 6. 7./* 8. * 用于 Gson 命名策略的注解 9. */ 10. Documented 11. Retention(RetentionPolicy.RUNTIME) 12. Target(ElementType.FIELD) 13. public interface ParamName 14. 15. String value(); 16. 将注解对需要网络传输 bean 进行标注:java view plaincopy1.public class requestBean implements Parcelable 2.public static final int RESPONSE_OK = 1; 3.ParamName(“id“) 4.private String mId; 5.ParamName(“name“) 6.private String mName; 7.ParamName(“result“) 8.private int mResult; 9.ParamName(“message“) 10. private String mMessage; 11. public int getResult() 12. return mResult; 13. 14. public String getMessage() 15. return mMessage; 16. 17. public String getId() 18. 19. return mId; 20. 21. public String getName() 22. 23. return mName; 24. 25. public boolean succeed() 26. return getResult() = RESPONSE_OK; 27. 28. 把定义好的注解封装进 gsonjava view plaincopy1.import com.google.gson.FieldNamingPolicy; 2.import com.google.gson.FieldNamingStrategy; 3.import com.google.gson.Gson; 4.import com.google.gson.GsonBuilder; 5. 6.import com.newandbie.privatecustomize.Config; 7.import com.newandbie.privatecustomize.annotate.ParamName; 8.import com.newandbie.privatecustomize.model.Gender; 9. 10. import java.lang.reflect.Field; 11. 12. /* 13. * 自定义的 Gson 14. */ 15. public class GsonUtils 16. 17. public static Gson newInstance() 18. GsonBuilder builder = new GsonBuilder(); 19. 20. builder.setFieldNamingStrategy(new AnnotateNaming(); 21. 22. return builder.create(); 23. 24. 25. private static class AnnotateNaming implements FieldNamingStrategy 26. 27. Override 28. public String translateName(Field field) 29. ParamName a = field.getAnnotation(ParamName.class); 30. return a != null ? a.value() : FieldNamingPolicy.IDENTITY.translateName(field); 31. 32. 33. java view plaincopy1.import com.squareup.okhttp.Cache; 2.import com.squareup.okhttp.OkHttpClient; 3. 4.import android.content.Context; 5. 6.import java.io.F
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号