关闭

Retrofit2 Post 乱码问题

标签: retrofitpost乱码Content-Type
966人阅读 评论(0) 收藏 举报
分类:

对服务器的储存数据(中文utf8)进行更改,之前用Okhttp3 也是提交后,数据乱码,百度查了资料说改下content-type, 加上

application/x-www-form-urlencoded; charset=utf-8

但是没有奏效,然后又换成httpclient 发送方式

今天用了retrofit请求时,尝试下能不能成功,不出乱码,结果。。。

@FormUrlEncoded
@POST("{user}/setDeviceName")
Observable<String> updateName(@Header("Authorization") String header,@Path("user") String user, @Field("deviceMac") String mac, @Field("type") String type, @Field("name") String name);
测试
String name = "test1";
String pwd = "111111";
String mac = "18:fe:34:d8:87:a6";
api.updateName(marge(name,pwd), name, mac,"6","测试")
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(System.out::println, throwable -> {
            System.out.println(throwable.getMessage());
        });
运行结果乱码了

did name  mac
4581 测试 18:fe:34:d8:87:a6

看下log
08-30 01:35:14.110 3144-3161/? D/OkHttp: --> POST http://xxx/test1/setDeviceName http/1.1
08-30 01:35:14.111 3144-3161/? D/OkHttp: Content-Type: application/x-www-form-urlencoded
08-30 01:35:14.111 3144-3161/? D/OkHttp: Content-Length: 84
08-30 01:35:14.111 3144-3161/? D/OkHttp: Authorization: Basic dGVzdDE6MTExMTEx
08-30 01:35:14.111 3144-3161/? D/OkHttp: deviceMac=18%3Afe%3A34%3Ad8%3A87%3Aa6&type=6&name=%E6%B5%8B%E8%AF%95
08-30 01:35:14.111 3144-3161/? D/OkHttp: --> END POST 

返回的log
08-30 01:35:15.565 3144-3161/com.king.retrofittest D/OkHttp: <-- 200 OK http://115.28.12.182:8080/beehive/rest/user/test1/setDeviceName (1454ms)
08-30 01:35:15.565 3144-3161/com.king.retrofittest D/OkHttp: Server: Apache-Coyote/1.1
08-30 01:35:15.565 3144-3161/com.king.retrofittest D/OkHttp: Content-Type: */*;charset=UTF-8
08-30 01:35:15.565 3144-3161/com.king.retrofittest D/OkHttp: Content-Length: 4
08-30 01:35:15.565 3144-3161/com.king.retrofittest D/OkHttp: Date: Tue, 30 Aug 2016 05:35:14 GMT
08-30 01:35:15.565 3144-3161/com.king.retrofittest D/OkHttp: true
08-30 01:35:15.565 3144-3161/com.king.retrofittest D/OkHttp: <-- END HTTP (4-byte body)

返回的内容类型有UTF8,听说okhttp3发送的Content-Type默认就是utf8的,但是请求的时候没有显示附带,不过retrofit可以设置请求头
@FormUrlEncoded
@POST("{user}/setDeviceName")
@Headers("Content-Type:application/x-www-form-urlencoded; charset=utf-8") //添加
Observable<String> updateName(@Header("Authorization") String header,@Path("user") String user
, @Field("deviceMac") String mac, @Field("type") String type, @Field("name") String name);


再次请求结果 已经成功了
did name mac
4581 测试 18:fe:34:d8:87:a6

LOG
08-30 01:45:14.008 12126-12148/? D/OkHttp: --> POST http://xxx/test1/setDeviceName http/1.1
08-30 01:45:14.008 12126-12148/? D/OkHttp: Content-Type: application/x-www-form-urlencoded; charset=utf-8
08-30 01:45:14.009 12126-12148/? D/OkHttp: Content-Length: 84
08-30 01:45:14.009 12126-12148/? D/OkHttp: Authorization: Basic dGVzdDE6MTExMTEx
08-30 01:45:14.009 12126-12148/? D/OkHttp: deviceMac=18%3Afe%3A34%3Ad8%3A87%3Aa6&type=6&name=%E6%B5%8B%E8%AF%95
08-30 01:45:14.009 12126-12148/? D/OkHttp: --> END POST 



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23393次
    • 积分:464
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类