retrofit熟悉

之前没用过retrofit,最近项目里引入了这个流行库,学习用吧,按照例子(sample
中的RequestBuilderTest.java),就拿公司的登录接口来测试。

首先,定义个request接口:

public interface LoginRequest {
    @POST("logon/login")
    Call<ResponseBody> getString();//ResponseBody代表直接获得返回的body
}

然后使用接口:


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.baseUrl)
                .build();

        LoginRequest requestServices = retrofit.create(LoginRequest.class);

        final Call<ResponseBody> call = requestServices.getString();

        // Fetch and print a list of the contributors to the library.

        new Thread(new Runnable() {
            @Override
            public void run() {
                ResponseBody responseBody = null;
                try {
                    responseBody = call.execute().body();
                    Log.e("=============", "responseBody"+responseBody.string().toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

运行,返回:
responseBody{“code”:500,“msg”:“com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input\n at [Source: org.apache.catalina.connector.CoyoteInputStream@76412093; line: 1, column: 1]”}

接下来添加请求的header和body参数。修改后如下:

public interface LoginRequest {
     @FormUrlEncoded
    @Headers("Content-Type:application/json")
    @POST("logon/login")
    Call<ResponseBody> login(@Field("uid") String name,
                             @Field("pwd") String pwd,
                             @Field("rid") String rid,
                             @Field("clientId") String clientId,
                             @Field("forAccessToken") boolean forAccessToken);
}

同时,请求的地方改成:

final Call<ResponseBody> call = requestServices.login("13588745152", stringToMD5("123456"), "doctor", "2865", true);

运行,发现报
responseBody{“code”:500,“msg”:“com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘uid’: was expecting (‘true’, ‘false’ or ‘null’)\n at [Source: org.apache.catalina.connector.CoyoteInputStream@26c8f101; line: 1, column: 5]”}

一脸懵逼,只好去打印它发出去的请求内容看看。加日志拦截器:


    private OkHttpClient okhttpclient() {
        if (mOkHttpClient == null) {
            HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
            logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            mOkHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .addNetworkInterceptor(logInterceptor)
                    .build();
        }
        return mOkHttpClient;
    }

打印出的结果如下:

08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: --> POST http://ngaribata.ngarihealth.com:8480/ehealth-base-devtest/logon/login http/1.1
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: Content-Type: application/json
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: Content-Length: 97
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: charset: UTF-8
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: Host: ngaribata.ngarihealth.com:8480
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: Connection: Keep-Alive
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: Accept-Encoding: gzip
08-30 15:56:11.748 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: User-Agent: okhttp/3.8.0
08-30 15:56:11.748 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: uid=13588745152&pwd=e10adc3949ba59abbe56e057f20f883e&rid=doctor&clientId=2865&forAccessToken=true
08-30 15:56:11.749 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: --> END POST (97-byte body)

参数不对。重新写另外一种:

@Headers({"Content-Type:application/json"
            ,"charset:UTF-8"})
    @POST("logon/login")
    Call<ResponseBody> login1(@Body UserRequestBean body);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.baseUrl)
                .client(mOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        LoginRequest requestServices = retrofit.create(LoginRequest.class);

        //final Call<ResponseBody> call = requestServices.login("\"13588745152\"", stringToMD5("123456"), "doctor", "2865", true);
        // Fetch and print a list of the contributors to the library.
        UserRequestBean bean = new UserRequestBean();
        bean.setUid("13588745152");
        bean.setPwd(stringToMD5("123456"));
        bean.setRid( "doctor");
        bean.setClientId("2865");
        bean.setforAccessToken(true);
        final Call<ResponseBody> call = requestServices.login1(bean);

返回正确值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值