android 网络框架-Retrofit(二)

Xml格式配置

你好,我是kpioneer
你好,我是Jeason
你好,我是Cook

注意:@Root:注解代表Xml根节点(需要在类上面使用)
例如:

  @Element:注解代表根节点中下面子节点
   例如:<user></user>
   
  @Attribute:注解代表标签页中的属性
   例如:userid = '1'
      
  @Text:注解代表标签内容
   例如:你好,我是kpioneer

解析器源码:

.addConverterFactory(xxxxFactory.create())
采用抽象工厂模式
抽象工厂模式是工厂方法模式的进一步延伸,由于它提供了功能更为强大的工厂类并且具备较好的可扩展性

public interface Converter<F, T> {
  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<SimpleResponse>}
     * declaration.
     */
    public @Nullable Converter<ResponseBody, ?> 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<?, RequestBody> 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<?, String> 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<String, ? extends Runnable>} 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<? extends Runnable>} 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 Observable<LoginBean> login(@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<LoginBean>() {

                    @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<LoginBean>() {
//                @Override
//                public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
//                    onHttpResultListener.onHttpResult(response.body());
//                }
//
//                @Override
//                public void onFailure(Call<LoginBean> 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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安卓兼职framework应用工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值