How to get JSON object without converting in Retro

2019-06-21 04:22发布

I need download json object from REST server without converting by GSON. But not understand how make in Retrofit 2.0 bata 1

3条回答
狗以群分
2楼-- · 2019-06-21 04:56

Simply use JsonElement as your pojo. For example

In your FlowerApi interafce:

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

In your main class:

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

Actually, the response is still converted to JsonElement by the Gson converter, but you can get something that is close to the raw response.

查看更多
萌系小妹纸
3楼-- · 2019-06-21 05:01

We can get JsonObject (com.google.gson) and parse this as well as standard JSONObject (org.json)

Interface FlowerApi

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

In rest handler use

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 class help a create retrofit service easy

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

In 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'
查看更多
你好瞎i
4楼-- · 2019-06-21 05:17

1 compile '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'

2 OkHttpClient 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);

查看更多
登录 后发表回答