美文网首页
Retrofit 2.0直接使用JSON进行数据交互

Retrofit 2.0直接使用JSON进行数据交互

作者: maiduoduo | 来源:发表于2020-07-28 15:36 被阅读0次

之前使用Retrofit都是将JSON串转化为POJO对象,针对不同的业务协议,定义相应的接口和参数列表。但是此种方式一般用在自己内部协议基础上,具体大的项目中,有些第三方的集成功能,一般都采用统一的方式即请求JSON和回应JSON进行数据交互,不可能每个第三方协议都会去定义与协议相应的POJO对象。

HTTP肯定有GET和POST方法,先定义Retrofit Api的interface:

package com.hdnetworklib.network.http;

import java.util.Map;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;

/**
 * Created by wangyuhang@evergrande.cn on 2017/8/23 0023.
 */

public interface RetrofitServiceApi {
    @POST
    Call<ResponseBody> reqPost(@Url String url, @Body RequestBody requestBody);

    @GET
    Call<ResponseBody> reqGet(@Url String url, @QueryMap Map<String, String> options);

    @GET
    Call<ResponseBody> reqGet(@Url String url);
}

1、POST方式,采用指定完整的URL,reqeustBody就是后面业务要传入的完整JSON串
2、GET方式,后面的options就是一个Map,业务参数键值就存在这个里面,URL里面不需要带值。
3、GET方式,与2不同的是没有options,这样就键值对全部带在URL里面,类似于这样的格式:http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1

接下来就是具体对业务的接口了,提供POST和GET两个请求接口调用:

package com.hdnetworklib.network.http;

import android.util.Log;

import java.io.IOException;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wangyuhang@evergrande.cn on 2017/7/12 0012.
 */

public class HttpClient {
    private static final String TAG = "HttpClient";
    private static volatile HttpClient instance;

    private HttpClient() {
    }

    public static HttpClient getInstance() {
        if (instance == null) {
            synchronized (HttpClient.class) {
                if (instance == null) {
                    instance = new HttpClient();
                }
            }
        }

        return instance;
    }

    /**
     * Http Post请求
     *
     * @param req_id   请求编号
     * @param method   请求业务方法
     * @param url      请求的URL
     * @param jsonData  POST需要所带参数(JSON串格式)
     * @param callback 回调接口
     */
    public void reqPostHttp(final int req_id, final String method, String url, String jsonData, final HttpCallback callback) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.what.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitServiceApi retrofitServiceApi = retrofit.create(RetrofitServiceApi.class);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonData);

        Call<ResponseBody> call = retrofitServiceApi.reqPost(url, body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String result = response.body().string();
                    Log.i(TAG, "reqPostHttp onResponse: " + result);

                    if (callback != null) {
                        callback.onSuccess(new HttpResMsg(req_id, method, result));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, "reqPostHttp onResponse exception: " + e.toString());

                    if (callback != null) {
                        callback.onError(e.toString());
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e(TAG, "reqPostHttp onFailure: " + t.toString());

                if (callback != null) {
                    callback.onError(t.toString());
                }
            }
        });
    }

    /**
     * Http Get请求
     *
     * @param req_id   请求编号
     * @param method   请求业务方法
     * @param url      请求的URL
     * @param options  GET需要所带参数键值(如果URL里带有则不需要在此添加)
     * @param callback 回调接口
     */
    public void reqGetHttp(final int req_id, final String method, String url,
                           Map<String, String> options, final HttpCallback callback) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.what.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitServiceApi retrofitServiceApi = retrofit.create(RetrofitServiceApi.class);

        Call<ResponseBody> call = null;
        if (options == null) {
            call = retrofitServiceApi.reqGet(url);
        } else {
            call = retrofitServiceApi.reqGet(url, options);
        }

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String result = response.body().string();
                    Log.i(TAG, "reqPostHttp onResponse: " + result);

                    if (callback != null) {
                        callback.onSuccess(new HttpResMsg(req_id, method, result));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, "reqPostHttp onResponse exception: " + e.toString());

                    if (callback != null) {
                        callback.onError(e.toString());
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e(TAG, "reqPostHttp onFailure: " + t.toString());
                if (callback != null) {
                    callback.onError(t.toString());
                }
            }
        });
    }
}

需要注意的是:

baseUrl("http://www.what.com/")

这里的这个baseUrl是我瞎掰的一个地址,因为Retrofit的限制:如果baseUrl不是以 / 结尾就会报异常:

Caused by: java.lang.IllegalArgumentException: baseUrl must end in /

当我们需要完整的指定URL的时候,特别是上面列出的第二种GET方式,我们的URL是http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1,如果我们直接通过接口传参把这个URL直接传入baseUrl中,如下(注意最后没有/结尾):

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

这样运行时就会报错。那如果我们手工在最后面加上一个/呢?如下(注意最后有/结尾):

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

这样运行时仍然报错,而且你把这个链接复制到浏览器中看看就知道肯定不行的:http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1/

我一开始遇到这个问题的时候也是第一反应去查Retrofit的官方文档和说明,或者让第三方的开发人员采用第二种GET请求方式,用一个以 / 结尾的URL,然后把URL中?后面带的那些值放到一个Map里传进来。首先官方说明和Api用法没找到,而且这个baseUrl还必须调用,其次,别的开发人员不愿意弄,好好的辛辛苦苦把URL都组装好了,没啥事让我传Map啊,肯定也不行。后面在这里找到了答案:https://stackoverflow.com/questions/36736854/retrofit2-how-do-i-put-the-at-the-end-of-the-dynamic-baseurl

image image

所以既然你后面会完整指定URL,那么一开始的baseUrl就无关紧要,随便写一个以/结尾的Http地址就可以了。

剩下的的就是回调和消息的组装了,各位可以根据自己的业务需求进行组装和调整,我这里就只贴出代码不做过多解析了。

回调接口:

package com.hdnetworklib.network.http;

/**
 * Created by wangyuhang@evergrande.cn on 2017/8/23 0023.
 */

public interface HttpCallback {
    void onSuccess(HttpResMsg httpResMsg);

    void onError(String errorMsg);
}

消息结构的组装:

package com.hdnetworklib.network.http;

/**
 * Created by wangyuhang@evergrande.cn on 2017/8/23 0023.
 */

public class HttpResMsg {
    private Integer req_id;
    private String method;
    private String data;

    public HttpResMsg() {
    }

    public HttpResMsg(int req_id, String method, String data) {
        this.req_id = req_id;
        this.method = method;
        this.data = data;
    }

    public Integer getReq_id() {
        return req_id;
    }

    public void setReq_id(Integer req_id) {
        this.req_id = req_id;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

转载自:
作者:平凡至简
链接:https://www.jianshu.com/p/1e5200898c7b

相关文章

  • Retrofit 2.0直接使用JSON进行数据交互

    之前使用Retrofit都是将JSON串转化为POJO对象,针对不同的业务协议,定义相应的接口和参数列表。但是此种...

  • Retrofit 2.0直接使用JSON进行数据交互

    之前使用Retrofit都是将JSON串转化为POJO对象,针对不同的业务协议,定义相应的接口和参数列表。但是此种...

  • json 的使用(更新中....)

    json的使用 学习目的: 1.掌握并能使用JSON数据格式进行通信交互 2...

  • Spring14-JSON

    为什么要使用json数据进行交互 json数据格式在接口调用中 html页面中比较常用,因为json格式比较简单,...

  • Retrofit + Gson解析数据容错处理

    使用Retrofit+Gson 处理网络数据,后台数据返回不规范,因为种种原因后台无法修改,直接解析会报错Json...

  • 初见SpringMVC之json交互

    初见SpringMVC之json交互 JSON数据是一种存储数据的格式,前后台之间通过json数据进行交互是非常常...

  • 优秀框架讲解汇总

    Retrofit 2.0 《Retrofit 2.0:有史以来最大的改进》 《使用 Retrofit 请求 AP...

  • Retrofit 2.0 解析JSON数据

    Retrofit 2.0 解析数据 我是一个自学Android和JavaWeb的菜鸟。如果可以Flower一下我...

  • PHP中JSON的使用

    学习目的 掌握并且能够使用json数据格式进行通信交互 理解json格式和serialize序列化、xml、数组的...

  • OkHttp 源码解析

    前言 OkHttp可以说是最主流的网络请求框架了,很多项目是直接使用Retrofit 2.0提供的接口进行网络请求...

网友评论

      本文标题:Retrofit 2.0直接使用JSON进行数据交互

      本文链接:https://www.haomeiwen.com/subject/twifrktx.html