【问题标题】:How to get JSON object without converting in Retrofit 2.0?如何在 Retrofit 2.0 中获取 JSON 对象而不进行转换?
【发布时间】:2019-05-29 06:09:28
【问题描述】:

我需要从 REST 服务器下载 json 对象,而不需要通过 GSON 进行转换。但不明白如何在 Retrofit 2.0 bata 1 中制作

【问题讨论】:

  • 你找到解决办法了吗?
  • @Shan Xeeshi 请看下面我的解决方案
  • 这个问题有什么更新吗?

标签: android json rest


【解决方案1】:

只需使用 JsonElement 作为您的 pojo。例如

在您的 FlowerApi 界面中:

    @GET("/flower")
    Call<JsonElement> getFlowers();

在你的主课中:

    Call<JsonElement> getFlowersCall = httpApiClass.getFlowers();

    getFlowersCall.enqueue(new Callback<JsonElement>() {
        @Override
        public void onResponse(Response<JsonElement> response, Retrofit retrofit) {
            JsonElement jsonElement = response.body();
            Log.d(TAG, jsonElement.toString());
        }

        @Override
        public void onFailure(Throwable t) {
            Log.d(TAG, "Failed: " + t.getMessage());
        }
    });

实际上,响应仍然是由 Gson 转换器转换为JsonElement,但您可以获得接近原始响应的东西。

【讨论】:

    【解决方案2】:

    1compile 'io.reactivex:rxjava:1.0.+' compile 'com.squareup.retrofit:retrofit:2.0.0-beta1' compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta1' compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1' compile 'com.squareup.okhttp:okhttp:2.5.0' compile 'com.squareup.okhttp:okhttp-urlconnection:2.5.0'

    2OkHttpClient client = new OkHttpClient(); // client.interceptors().add(new UrlInterceptor()); client.interceptors().add(new LoggingInterceptor()); AppApi api = new Retrofit.Builder() .baseUrl("https://api.github.com") // add a converter for String .addConverter(String.class, new ToStringConverter()) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(client) .build() .create(AppApi.class);

    【讨论】:

    • 你能解释一下 ToStringConverter() 吗?
    【解决方案3】:

    我们可以得到 JsonObject (com.google.gson) 并解析它以及标准 JSONObject (org.json)

    接口 FlowerApi

    @GET("/flower")
    Call<JsonObject> getFlowers();
    

    在休息处理程序中使用

    FlowerApi api = ApiFactory.createRetrofitService(FlowerApi.class);
    Call<JsonObject> call = api.getFlowers();
    retrofit.Response response = call.execute();
    if (response.isSuccess()) {
       JsonObject object = (JsonObject) response.body();
    }
    

    Api Factory 类帮助轻松创建改造服务

    public class ApiFactory {
    
        private static final int TIMEOUT = 60;
        private static final int WRITE_TIMEOUT = 120;
        private static final int CONNECT_TIMEOUT = 10;
    
        private static final OkHttpClient CLIENT = new OkHttpClient();
    
        private static final String BASE_URL = "flowers.com";
    
        static {
            CLIENT.setConnectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS);
            CLIENT.setWriteTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS);
            CLIENT.setReadTimeout(TIMEOUT, TimeUnit.SECONDS);
        }
    
        @NonNull
        public static <S> S createRetrofitService(Class<S> serviceClass) {
            return getRetrofit().create(serviceClass);
        }
    
        @NonNull
        private static Retrofit getRetrofit() {
            return new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(CLIENT)
                    .build();
        }
    }
    

    在 build.gradle 中

        compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
        compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
        compile 'com.squareup.okhttp:okhttp:2.0.0'
    

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多