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

之前使用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,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,"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


所以既然你后面会完整指定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;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

以上是来客网为你收集整理的Android中Retrofit 2.0直接使用JSON进行数据交互全部内容,希望文章能够帮你解决Android中Retrofit 2.0直接使用JSON进行数据交互所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。