
io.apptik.comm.jus.request.GsonRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jus-gson Show documentation
Show all versions of jus-gson Show documentation
Communication library for Android and Java
/*
* Copyright (C) 2015 AppTik Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.apptik.comm.jus.request;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import io.apptik.comm.jus.NetworkRequest;
import io.apptik.comm.jus.converter.GsonRequestConverter;
import io.apptik.comm.jus.converter.GsonResponseConverter;
import io.apptik.comm.jus.Request;
import io.apptik.comm.jus.http.HttpUrl;
public class GsonRequest extends Request {
public GsonRequest(String method, HttpUrl url, TypeAdapter typeAdapter) {
super(method, url, new GsonResponseConverter<>(typeAdapter));
setNetworkRequest(NetworkRequest.Builder.from(getNetworkRequest())
.setHeader("Accept", "application/json")
.build());
}
public GsonRequest(String method, HttpUrl url, Class tClass, Gson gson) {
this(method, url, gson.getAdapter(tClass));
}
public GsonRequest(String method, HttpUrl url, Class tClass) {
this(method, url, tClass, new Gson());
}
public GsonRequest(String method, String url, TypeAdapter typeAdapter) {
super(method, url, new GsonResponseConverter<>(typeAdapter));
setNetworkRequest(NetworkRequest.Builder.from(getNetworkRequest())
.setHeader("Accept", "application/json")
.build());
}
public GsonRequest(String method, String url, Class tClass, Gson gson) {
this(method, url, gson.getAdapter(tClass));
}
public GsonRequest(String method, String url, Class tClass) {
this(method, url, tClass, new Gson());
}
public GsonRequest setRequestData(R requestData, Gson gson, TypeAdapter adapter) {
try {
super.setRequestData(requestData, new GsonRequestConverter<>(gson, adapter));
} catch (IOException e) {
throw new RuntimeException("Unable to convert " + requestData + " to NetworkRequest", e);
}
setNetworkRequest(NetworkRequest.Builder.from(getNetworkRequest())
.setHeader("Accept", "application/json")
.build());
return this;
}
public GsonRequest setRequestData(R requestData, Gson gson) {
TypeAdapter> adapter;
if(requestData==null) {
adapter = gson.getAdapter(Object.class);
} else {
adapter = gson.getAdapter(TypeToken.get(requestData.getClass()));
}
return setRequestData(requestData, gson, adapter);
}
public GsonRequest setRequestData(R requestData) {
return setRequestData(requestData, new Gson());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy