Xml格式配置 你好,我是kpioneer 你好,我是Jeason 你好,我是Cook
注意:@Root:注解代表Xml根节点(需要在类上面使用) 例如:
@Element:注解代表根节点中下面子节点 例如:@Attribute:注解代表标签页中的属性 例如:userid = '1' @Text:注解代表标签内容 例如:你好,我是kpioneer复制代码
解析器源码:
.addConverterFactory(xxxxFactory.create()) 采用抽象工厂模式 抽象工厂模式是工厂方法模式的进一步延伸,由于它提供了功能更为强大的工厂类并且具备较好的可扩展性
public interface Converter{ T convert(F value) throws IOException; /** Creates {@link Converter} instances based on a type and target usage. */ abstract class Factory { /** * Returns a {@link Converter} for converting an HTTP response body to {@code type}, or null if * {@code type} cannot be handled by this factory. This is used to create converters for * response types such as {@code SimpleResponse} from a {@code Call } * declaration. */ public @Nullable Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { return null; } /** * Returns a {@link Converter} for converting {@code type} to an HTTP request body, or null if * {@code type} cannot be handled by this factory. This is used to create converters for types * specified by {@link Body @Body}, {@link Part @Part}, and {@link PartMap @PartMap} * values. */ public @Nullable Converter requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { return null; } /** * Returns a {@link Converter} for converting {@code type} to a {@link String}, or null if * {@code type} cannot be handled by this factory. This is used to create converters for types * specified by {@link Field @Field}, {@link FieldMap @FieldMap} values, * {@link Header @Header}, {@link HeaderMap @HeaderMap}, {@link Path @Path}, * {@link Query @Query}, and {@link QueryMap @QueryMap} values. */ public @Nullable Converter stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) { return null; } /** * Extract the upper bound of the generic parameter at {@code index} from {@code type}. For * example, index 1 of {@code Map } returns {@code Runnable}. */ protected static Type getParameterUpperBound(int index, ParameterizedType type) { return Utils.getParameterUpperBound(index, type); } /** * Extract the raw class type from {@code type}. For example, the type representing * {@code List } returns {@code List.class}. */ protected static Class getRawType(Type type) { return Utils.getRawType(type); } }}复制代码
简单区分: 抽象工厂模式:一组相关性很高的方法 工厂方法模式:一个方法
Retrofit框架+RxJava开发 配置RxJava环境
compile 'io.reactivex:rxjava:1.3.0' compile 'io.reactivex:rxandroid:1.2.1'复制代码
public interface RxJavaLoginService { @FormUrlEncoded @POST("user/login?platform=android&city_id=101&type=pwd&channel=baiduxi&version=3.2.0&os_version=6.0.1&device_id=866622020797175") public Observablelogin(@Field("mobile") String username, @Field("password")String password);}复制代码
public class SimpleRetrofitRxJava { private static String URL_SERVER = "http://api.cloud.test.haocaisong.cn/v2.0/"; /** * 异步:采用Retrofit框架自带的 * * @param username * @param password * @param onHttpResultListener */ public static void login(String username, String password, final SimpleSystemLogin.OnHttpResultListener onHttpResultListener) { RxJavaLoginService loginService = getLoginService(); loginService.login(username, password).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(LoginBean loginBean) { onHttpResultListener.onHttpResult(loginBean.getErrmsg()); } });// try {// callLogin.enqueue(new Callback () {// @Override// public void onResponse(Call call, Response response) {// onHttpResultListener.onHttpResult(response.body());// }//// @Override// public void onFailure(Call call, Throwable t) {// onHttpResultListener.onHttpResult("登录失败!");// }// });// }catch (Exception e){// e.printStackTrace();// } } @NonNull private static OkHttpClient getOkHttpClient() { HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); return new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build(); } private static RxJavaLoginService getLoginService() { OkHttpClient okHttpClient = getOkHttpClient(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(URL_SERVER) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(okHttpClient) .build(); return retrofit.create(RxJavaLoginService.class); }}复制代码
核心代码分析: 动态代理和注解方式实现