最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中Retrofit 2.0直接使用JSON進(jìn)行數(shù)據(jù)交互

 更新時(shí)間:2017年08月24日 09:00:08   作者:平凡至簡(jiǎn)  
本篇文章主要介紹了Android中Retrofit 2.0直接使用JSON進(jìn)行數(shù)據(jù)交互,具有一定的參考價(jià)值,有興趣的可以了解一下

之前使用Retrofit都是將JSON串轉(zhuǎn)化為POJO對(duì)象,針對(duì)不同的業(yè)務(wù)協(xié)議,定義相應(yīng)的接口和參數(shù)列表。但是此種方式一般用在自己內(nèi)部協(xié)議基礎(chǔ)上,具體大的項(xiàng)目中,有些第三方的集成功能,一般都采用統(tǒng)一的方式即請(qǐng)求JSON和回應(yīng)JSON進(jìn)行數(shù)據(jù)交互,不可能每個(gè)第三方協(xié)議都會(huì)去定義與協(xié)議相應(yīng)的POJO對(duì)象。

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就是后面業(yè)務(wù)要傳入的完整JSON串

2、GET方式,后面的options就是一個(gè)Map,業(yè)務(wù)參數(shù)鍵值就存在這個(gè)里面,URL里面不需要帶值。

3、GET方式,與2不同的是沒有options,這樣就鍵值對(duì)全部帶在URL里面,類似于這樣的格式:http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1

接下來就是具體對(duì)業(yè)務(wù)的接口了,提供POST和GET兩個(gè)請(qǐng)求接口調(diào)用:

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請(qǐng)求
   *
   * @param req_id  請(qǐng)求編號(hào)
   * @param method  請(qǐng)求業(yè)務(wù)方法
   * @param url   請(qǐng)求的URL
   * @param jsonData POST需要所帶參數(shù)(JSON串格式)
   * @param callback 回調(diào)接口
   */
  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請(qǐng)求
   *
   * @param req_id  請(qǐng)求編號(hào)
   * @param method  請(qǐng)求業(yè)務(wù)方法
   * @param url   請(qǐng)求的URL
   * @param options GET需要所帶參數(shù)鍵值(如果URL里帶有則不需要在此添加)
   * @param callback 回調(diào)接口
   */
  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());
        }
      }
    });
  }
}

需要注意的是:

這里的這個(gè)baseUrl是我瞎掰的一個(gè)地址,因?yàn)镽etrofit的限制:如果baseUrl不是以 / 結(jié)尾就會(huì)報(bào)異常:

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

當(dāng)我們需要完整的指定URL的時(shí)候,特別是上面列出的第二種GET方式,我們的URL是http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1,如果我們直接通過接口傳參把這個(gè)URL直接傳入baseUrl中,如下(注意最后沒有/結(jié)尾):

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

這樣運(yùn)行時(shí)就會(huì)報(bào)錯(cuò)。那如果我們手工在最后面加上一個(gè)/呢?如下(注意最后有/結(jié)尾):

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

這樣運(yùn)行時(shí)仍然報(bào)錯(cuò),而且你把這個(gè)鏈接復(fù)制到瀏覽器中看看就知道肯定不行的:http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1/

我一開始遇到這個(gè)問題的時(shí)候也是第一反應(yīng)去查Retrofit的官方文檔和說明,或者讓第三方的開發(fā)人員采用第二種GET請(qǐng)求方式,用一個(gè)以 / 結(jié)尾的URL,然后把URL中?后面帶的那些值放到一個(gè)Map里傳進(jìn)來。首先官方說明和Api用法沒找到,而且這個(gè)baseUrl還必須調(diào)用,其次,別的開發(fā)人員不愿意弄,好好的辛辛苦苦把URL都組裝好了,沒啥事讓我傳Map啊,肯定也不行。后面在這里找到了答案:https://stackoverflow.com/questions/36736854/retrofit2-how-do-i-put-the-at-the-end-of-the-dynamic-baseurl


所以既然你后面會(huì)完整指定URL,那么一開始的baseUrl就無關(guān)緊要,隨便寫一個(gè)以/結(jié)尾的Http地址就可以了。

剩下的的就是回調(diào)和消息的組裝了,各位可以根據(jù)自己的業(yè)務(wù)需求進(jìn)行組裝和調(diào)整,我這里就只貼出代碼不做過多解析了。

回調(diào)接口:

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);
}

消息結(jié)構(gòu)的組裝:

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;
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

昂仁县| 湖南省| 白朗县| 江门市| 郓城县| 岳西县| 南丰县| 岑溪市| 翼城县| 广河县| 清涧县| 益阳市| 华容县| 休宁县| 彭泽县| 花垣县| 张家川| 辉南县| 如东县| 临朐县| 广南县| 二连浩特市| 丰城市| 库车县| 五原县| 冷水江市| 永顺县| 乌拉特中旗| 河北区| 云龙县| 湖州市| 涪陵区| 鹰潭市| 缙云县| 龙川县| 二手房| 饶河县| 江陵县| 宿松县| 那曲县| 金门县|