Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get JSON object without converting in Retrofit 2.0?

Tags:

json

rest

android

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

like image 474
user3910670 Avatar asked Sep 05 '15 18:09

user3910670


3 Answers

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.

like image 176
hackjutsu Avatar answered Oct 05 '22 14:10

hackjutsu


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

like image 44
zhiping Yin Avatar answered Oct 05 '22 13:10

zhiping Yin


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'
like image 28
user3910670 Avatar answered Oct 05 '22 14:10

user3910670