I want to create a JacksonJSONRequest implementation in Volley. My requests/responses like most will have a request object of type X, and a response object of type Y.
The Volley Request base class defines both as the same...
public class JacksonRequest<T> extends Request<T>
...
protected Response<T> parseNetworkResponse(NetworkResponse response)
This doesn't make much sense to me. I can't imagine many REST requests using the same structure for request and responses.
Am I missing something obvious here?
Here is my implementation...
public class JacksonRequest<T> extends JsonRequest<T>
{
private Class<T> responseType;
/**
* Creates a new request.
*
* #param method
* the HTTP method to use
* #param url
* URL to fetch the JSON from
* #param requestData
* A {#link Object} to post and convert into json as the request. Null is allowed and indicates no parameters will be posted along with request.
* #param listener
* Listener to receive the JSON response
* #param errorListener
* Error listener, or null to ignore errors.
*/
public JacksonRequest(int method, String url, Object requestData, Class<T> responseType, Listener<T> listener, ErrorListener errorListener)
{
super(method, url, (requestData == null) ? null : Mapper.string(requestData), listener, errorListener);
this.responseType = responseType;
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response)
{
try
{
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(Mapper.objectOrThrow(jsonString, responseType), HttpHeaderParser.parseCacheHeaders(response));
}
catch (Exception e)
{
return Response.error(new ParseError(e));
}
}
}
The Mapper class which the above uses is just a small wrapper class...
/**
* Singleton wrapper class which configures the Jackson JSON parser.
*/
public final class Mapper
{
private static ObjectMapper MAPPER;
public static ObjectMapper get()
{
if (MAPPER == null)
{
MAPPER = new ObjectMapper();
// This is useful for me in case I add new object properties on the server side which are not yet available on the client.
MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
return MAPPER;
}
public static String string(Object data)
{
try
{
return get().writeValueAsString(data);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public static <T> T objectOrThrow(String data, Class<T> type) throws JsonParseException, JsonMappingException, IOException
{
return get().readValue(data, type);
}
public static <T> T object(String data, Class<T> type)
{
try
{
return objectOrThrow(data, type);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
Not sure this is going to help you, but I have a jackson request that take a type and return a string, I use it because most of my requests just return a header
Edit
This have different request and response types, it's not super elegant but it works
public class JacksonJsonRequest<K, T> extends Request<T> {
private ObjectMapper mMapper = new ObjectMapper();
private static final String PROTOCOL_CHARSET = "utf-8";
private static final String PROTOCOL_CONTENT_TYPE =
String.format("application/json; charset=%s", PROTOCOL_CHARSET);
private final Response.Listener<T> mListener;
private final K mRequestBody;
private final Class<T> mClass;
private final Map<String, String> mHeaders;
public JacksonJsonRequest(int method, String url, K requestBody,
Response.ErrorListener errorListener, Response.Listener<T> listener,
Map<String, String> headers, Class<T> claz) {
super(method, url, errorListener);
mListener = listener;
mRequestBody = requestBody;
mHeaders = headers;
mClass = claz;
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse networkResponse) {
String jsonString = new String(networkResponse.data);
try {
T result = mMapper.readValue(jsonString, mClass);
return Response.success(result, HttpHeaderParser.parseCacheHeaders(networkResponse));
} catch (IOException e) {
Log.e("jacksontest", "error parsing", e);
}
return null;
}
#Override
protected void deliverResponse(T t) {
mListener.onResponse(t);
}
#Override
public String getBodyContentType() {
return PROTOCOL_CONTENT_TYPE;
}
#Override
public byte[] getBody() {
try {
return mRequestBody == null ? null : mMapper.writeValueAsBytes(mRequestBody);
} catch (JsonProcessingException e) {
Log.e("Jacksontest", "error parsing", e);
}
return null;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return (mHeaders == null) ? super.getHeaders() : mHeaders;
}
}
Related
I am using Google's Volley to make GET and POST requests using the following custom request class:
public class GsonRequest<T> extends Request<T> {
private static final int SOCKET_TIMEOUT_MS = 30000;
private static final int MAX_RETRIES = 3;
private final Gson gson = new Gson();
private final Type type;
private final Map<String, String> params;
private final Response.Listener<T> listener;
/**
* Make a GET request and return a parsed object from JSON.
*
* #param url URL of the request to make
* #param type Relevant type object, for Gson's reflection
* #param params Map of request params
*/
public GsonRequest(int method, String url, Type type, Map<String, String> params,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.type = type;
this.params = params;
this.listener = listener;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers;
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
// Here is my question, can I add a param value as JSONArray? like this:
params.put("orderValue", "35");
params.put("price", ""price": ["13.00", "22.00"]");
return params != null ? params : super.getParams();
}
#Override
public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
final RetryPolicy policy = new DefaultRetryPolicy(SOCKET_TIMEOUT_MS, MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
return super.setRetryPolicy(policy);
}
#Override
public String getBodyContentType() {
return "application/json";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
Log.i("" + gson.toJson(params).getBytes("utf-8"));
return gson.toJson(params).getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", gson.toJson(params), "utf-8");
return super.getBody();
}
}
#Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
final String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return (Response<T>) Response.success(gson.fromJson(json, type), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
}
The Json I'm trying to send:
{
"orderValue": "35",
"price": [
"13.00",
"22.00"
]
}
What is really sent as I get from getBody() method log is:
{
"price": "[\"23.00\",\"55.00\"]",
"orderValue": "35"
}
Any help with this issue?
You need change extends Request to ** JsonObjectRequest** or create JSONArrayRequest object.
public class VolleyJSONObjectRequest extends JsonObjectRequest {
private Context context;
private int timeOut = 10000;
private int maxRetries = 1;
public VolleyJSONObjectRequest(int method, Context context, String url, JSONObject jsonObject, Listener<JSONObject> listener, ErrorListener errorListener) {
super(method, url, jsonObject, listener, errorListener);
super.setRetryPolicy(new DefaultRetryPolicy(timeOut, maxRetries, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
public void startRequest() {
VolleyHandler.get(context).addToRequestQueue(this);
}
}
Example method:
private void saveItems(){
if (itens != null && itens.size() > 0) {
try {
JSONArray itensJson = new JSONArray();
for (SalesOrderItem item : itens) { // your loop
JSONObject jsonObject = new JSONObject();
jsonObject.put("price", this.priceOne);
jsonObject.put("price", this.priceTwo);
itensJson.put(jsonObject);
}
JSONObject headerJSON = new JSONObject();
headerJSON.put("price", itensJson);
VolleyJSONObjectRequest request = new VolleyJSONObjectRequest(Request.Method.POST, context, context.getString(R.string.URL_SALES_ORDER_ITENS_INSERT), headerJSON, onResponseItem, onErrorResponseItem);
request.startRequest();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
request = new VolleyStringRequest(context, context.getString(R.string.URL_FINISH_SALES_ORDER_DRAFT), onResponseFinishSalesOrderDraft, onErrorResponseFinishSalesOrderDraft);
request.startRequest();
hello.
I want to make a post request using gson.
I got the class implemented into android website...
http://developer.android.com/intl/pt-br/training/volley/request-custom.html
UserRequestHelper.userRequest(Request.Method.POST, EndpointURL.USUARIO, null, new Response.Listener<Usuario>() {
#Override
public void onResponse(Usuario response) {
Toast.makeText(getActivity(), "Cadastro realizado com sucesso!", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "Erro ao realizar cadastro.", Toast.LENGTH_SHORT).show();
}
});
I need a body to send the user? How i send the object user to make a post request?
Could someone help me with this?
you have to override getBody :
#Override
public byte[] getBody() throws AuthFailureError {
return gson.toJson(dataIn).getBytes();
}
the whole example class(form the same link) edited:
public class GsonRequest<T> extends Request<T> {
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Map<String, String> headers;
private final Response.Listener<T> listener;
private final Object dataIn;
/**
* Make a GET request and return a parsed object from JSON.
*
* #param url URL of the request to make
* #param clazz Relevant class object, for Gson's reflection
* #param headers Map of request headers
*/
public GsonRequest(String url, Object dataIn, Class<T> clazz, Map<String, String> headers,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(Method.POST, url, errorListener);
this.dataIn = dataIn;
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
#Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
#Override
public byte[] getBody() throws AuthFailureError {
return gson.toJson(dataIn).getBytes();
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(
gson.fromJson(json, clazz),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
}
I am using volley for simple REST request and passing some post parameters in it. But It always giving response BasicNetwork.performRequest: Unexpected response code 400.
I have tried following custom request.
public class GsonRequestRest<T> extends Request<T>{
private static final String PROTOCOL_CHARSET = "utf-8";
private static final String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", PROTOCOL_CHARSET);
private final Listener<T> mListener;
private final String mRequestBody;
private Gson mGson;
private Class<T> mJavaClass;
private Map<String, String> params;
public GsonRequestLoginRest(int method, String url, Class<T> cls, String requestBody, Map<String, String> params,Listener<T> listener,ErrorListener errorListener) {
super(method, url, errorListener);
mGson = new Gson();
mJavaClass = cls;
mListener = listener;
mRequestBody = requestBody;
this.params = params;
}
#Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
return params;
};
#Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
private Map<String, String> headers = new HashMap<String, String>();
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers;
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
T parsedGSON = mGson.fromJson(jsonString, mJavaClass);
return Response.success(parsedGSON,HttpHeaderParser.parseCacheHeaders(response));
} catch (IOException e) {
Log.d("tag", e.getMessage());
return Response.error(new ParseError(e));
} catch (JsonSyntaxException je) {
return Response.error(new ParseError(je));
}
}
#Override
public String getBodyContentType() {
return PROTOCOL_CONTENT_TYPE;
}
#Override
public byte[] getBody() {
try {
return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",mRequestBody, PROTOCOL_CHARSET);
return null;
}
}
//In your extended request class
#Override
protected VolleyError parseNetworkError(VolleyError volleyError){
if(volleyError.networkResponse != null && volleyError.networkResponse.data != null){
VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
volleyError = error;
}
return volleyError;
}
}
I did try to override getParams() and pass parameters in it. But it was not passing parameters and throwing error 400. I tried many of suggestions here and here but nothing worked for me. Is there any bug in Volley. ?
Please guide.
it working for me when use getBody() instead of getParams() in Request class.
another thing you should use JSONObject instead of HashMap
to put your params
.
then convert the JSONObject to string and convert string to byte in the getBody method.
use this link for more explanation.
How to send a POST request using volley with string body?
Hope my answer clarified enough to you.
I have the following json response
{
"tag": [
{
"listing_count": 5,
"listings": [
{
"source": "source1",
"data": {
"image": "image1",
"name": "name1"
},
"name": "name1"
}
]
},
{
"listing_count": 5,
"listings": [
{
"source": "source2",
"data": {
"image": "imag2",
"name": "name2"
},
"name": "name2"
}
]
}
]
}
I have created the following classes for GSON request. How do I make the GSON request and store the values for the response using a volley request.
What should the GSON request be like?
public class TagList {
ArrayList<Tag> tags;
public static class Tag {
int listing_count;
ArrayList<Listings> listings;
public int getListing_count() {
return listing_count;
}
public void setListing_count(int listing_count) {
this.listing_count = listing_count;
}
public ArrayList<Listings> getListings() {
return listings;
}
public void setListings(ArrayList<Listings> listings) {
this.listings = listings;
}
}
public static class Listings {
String source;
Data data;
String name;
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class Data {
String image;
String name;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Just create a GsonRequest Class as follows (taken from Android Developer Docs)
public class GsonRequest<T> extends Request<T> {
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Map<String, String> headers;
private final Listener<T> listener;
/**
* Make a GET request and return a parsed object from JSON.
*
* #param url URL of the request to make
* #param clazz Relevant class object, for Gson's reflection
* #param headers Map of request headers
*/
public GsonRequest(String url, Class<T> clazz, Map<String, String> headers,
Listener<T> listener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
#Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(
gson.fromJson(json, clazz),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
}
Now in your class file (Activity) just call the this class as follows:
RequestQueue queue = MyVolley.getRequestQueue();
GsonRequest<MyClass> myReq = new GsonRequest<MyClass>(Method.GET,
"http://JSONURL/",
TagList.class,
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
We also need to create two methods -
createMyReqSuccessListener() - receive the response from GsonRequest
createMyReqErrorListener() - to handle any error
as follows:
private Response.Listener<MyClass> createMyReqSuccessListener() {
return new Response.Listener<MyClass>() {
#Override
public void onResponse(MyClass response) {
// Do whatever you want to do with response;
// Like response.tags.getListing_count(); etc. etc.
}
};
}
and
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Do whatever you want to do with error.getMessage();
}
};
}
I hope it will make some sense.
Here some useful code snippets.
GsonRequest for GET petitions:
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
/**
* Convert a JsonElement into a list of objects or an object with Google Gson.
*
* The JsonElement is the response object for a {#link com.android.volley.Request.Method} GET call.
*
* #author https://plus.google.com/+PabloCostaTirado/about
*/
public class GsonGetRequest<T> extends Request<T>
{
private final Gson gson;
private final Type type;
private final Response.Listener<T> listener;
/**
* Make a GET request and return a parsed object from JSON.
*
* #param url URL of the request to make
* #param type is the type of the object to be returned
* #param listener is the listener for the right answer
* #param errorListener is the listener for the wrong answer
*/
public GsonGetRequest
(String url, Type type, Gson gson,
Response.Listener<T> listener, Response.ErrorListener errorListener)
{
super(Method.GET, url, errorListener);
this.gson = gson;
this.type = type;
this.listener = listener;
}
#Override
protected void deliverResponse(T response)
{
listener.onResponse(response);
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response)
{
try
{
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return (Response<T>) Response.success
(
gson.fromJson(json, type),
HttpHeaderParser.parseCacheHeaders(response)
);
}
catch (UnsupportedEncodingException e)
{
return Response.error(new ParseError(e));
}
catch (JsonSyntaxException e)
{
return Response.error(new ParseError(e));
}
}
}
GsonRequest for POST petitions:
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
/**
* Convert a JsonElement into a list of objects or an object with Google Gson.
*
* The JsonElement is the response object for a {#link com.android.volley.Request.Method} POST call.
*
* #author https://plus.google.com/+PabloCostaTirado/about
*/
public class GsonPostRequest<T> extends JsonRequest<T>
{
private final Gson gson;
private final Type type;
private final Response.Listener<T> listener;
/**
* Make a GET request and return a parsed object from JSON.
*
* #param url URL of the request to make
* #param type is the type of the object to be returned
* #param listener is the listener for the right answer
* #param errorListener is the listener for the wrong answer
*/
public GsonPostRequest
(String url, String body, Type type, Gson gson,
Response.Listener<T> listener, Response.ErrorListener errorListener)
{
super(Method.POST, url, body, listener, errorListener);
this.gson = gson;
this.type = type;
this.listener = listener;
}
#Override
protected void deliverResponse(T response)
{
listener.onResponse(response);
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response)
{
try
{
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return (Response<T>) Response.success
(
gson.fromJson(json, type),
HttpHeaderParser.parseCacheHeaders(response)
);
}
catch (UnsupportedEncodingException e)
{
return Response.error(new ParseError(e));
}
catch (JsonSyntaxException e)
{
return Response.error(new ParseError(e));
}
}
}
This is how you use it for JSON objects:
/**
* Returns a dummy object
*
* #param listener is the listener for the correct answer
* #param errorListener is the listener for the error response
*
* #return #return {#link com.sottocorp.sotti.okhttpvolleygsonsample.api.GsonGetRequest}
*/
public static GsonGetRequest<DummyObject> getDummyObject
(
Response.Listener<DummyObject> listener,
Response.ErrorListener errorListener
)
{
final String url = "http://www.mocky.io/v2/55973508b0e9e4a71a02f05f";
final Gson gson = new GsonBuilder()
.registerTypeAdapter(DummyObject.class, new DummyObjectDeserializer())
.create();
return new GsonGetRequest<>
(
url,
new TypeToken<DummyObject>() {}.getType(),
gson,
listener,
errorListener
);
}
This is how you use it for JSON arrays:
/**
* Returns a dummy object's array
*
* #param listener is the listener for the correct answer
* #param errorListener is the listener for the error response
*
* #return {#link com.sottocorp.sotti.okhttpvolleygsonsample.api.GsonGetRequest}
*/
public static GsonGetRequest<ArrayList<DummyObject>> getDummyObjectArray
(
Response.Listener<ArrayList<DummyObject>> listener,
Response.ErrorListener errorListener
)
{
final String url = "http://www.mocky.io/v2/5597d86a6344715505576725";
final Gson gson = new GsonBuilder()
.registerTypeAdapter(DummyObject.class, new DummyObjectDeserializer())
.create();
return new GsonGetRequest<>
(
url,
new TypeToken<ArrayList<DummyObject>>() {}.getType(),
gson,
listener,
errorListener
);
}
This is how you use it for POST calls:
/**
* An example call (not used in this example app) to demonstrate how to do a Volley POST call
* and parse the response with Gson.
*
* #param listener is the listener for the success response
* #param errorListener is the listener for the error response
*
* #return {#link com.sottocorp.sotti.okhttpvolleygsonsample.api.GsonPostRequest}
*/
public static GsonPostRequest getDummyObjectArrayWithPost
(
Response.Listener<DummyObject> listener,
Response.ErrorListener errorListener
)
{
final String url = "http://PostApiEndpoint";
final Gson gson = new GsonBuilder()
.registerTypeAdapter(DummyObject.class, new DummyObjectDeserializer())
.create();
final JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "Ficus");
jsonObject.addProperty("surname", "Kirkpatrick");
final JsonArray squareGuys = new JsonArray();
final JsonObject dev1 = new JsonObject();
final JsonObject dev2 = new JsonObject();
dev1.addProperty("name", "Jake Wharton");
dev2.addProperty("name", "Jesse Wilson");
squareGuys.add(dev1);
squareGuys.add(dev2);
jsonObject.add("squareGuys", squareGuys);
return new GsonPostRequest<>
(
url,
jsonObject.toString(),
new TypeToken<DummyObject>()
{
}.getType(),
gson,
listener,
errorListener
);
}
}
All the code is taken from here, and you have a blog post about how to use OkHttp, Volley and Gson here.
I just made a custom json request which is based on Jackson library instead of Gson.
One thing I want to point out (it took my many hours to figure out...): if you want to support POST Json parameter as well, you should extend from JsonRequest instead of Request. Otherwise your Json request body will be url-encoded, on the server side you cannot convert it back to java object.
Here is my json request class, which is based on Jackson and supports Json parameter and header:
public class JacksonRequest<ResponseType> extends JsonRequest<ResponseType> {
private final ObjectMapper objectMapper = new ObjectMapper();
private final Class<ResponseType> responseClass;
private final Map<String, String> headers;
private String requestBody = null;
private static final String PROTOCOL_CHARSET = "utf-8";
/**
* POST method without header
*/
public JacksonRequest(String url,
Object parameterObject,
Class<ResponseType> responseClass,
Response.Listener<ResponseType> listener,
Response.ErrorListener errorListener) {
this(Method.POST, url, null, parameterObject, responseClass, listener, errorListener);
}
/**
* #param method see also com.android.volley.Request.Method
*/
public JacksonRequest(int method,
String url,
Map<String, String> headers,
Object parameterObject,
Class<ResponseType> responseClass,
Response.Listener<ResponseType> listener,
Response.ErrorListener errorListener) {
super(method, url, null, listener, errorListener);
if (parameterObject != null)
try {
this.requestBody = objectMapper.writeValueAsString(parameterObject);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
this.headers = headers;
this.responseClass = responseClass;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
#Override
protected Response<ResponseType> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
ResponseType result = objectMapper.readValue(json, responseClass);
return Response.success(result, HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonMappingException e) {
return Response.error(new ParseError(e));
} catch (JsonParseException e) {
return Response.error(new ParseError(e));
} catch (IOException e) {
return Response.error(new ParseError(e));
}
}
/**
* Cannot call objectMapper.writeValueAsString() before super constructor, so override the same getBody() here.
*/
#Override
public byte[] getBody() {
try {
return requestBody == null ? null : requestBody.getBytes(PROTOCOL_CHARSET);
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
requestBody, PROTOCOL_CHARSET);
return null;
}
}
}
anyone has an example, where you read a protobuf, using volley?. I have read that you can implement ProtobufRequest class, but I can not find documentation for it. Thank you for your help.
I have done it,the work is ok,hope it is helpful.first create the ProtobufRequest class like below that extends Request,Request is the basic request class in Volley,then,you can create the custom request extends ProtobufRequest that do custom doParse().just as a reference.
RequestData is SomeProto message,
private static final int SOCKET_TIMEOUT = 30000;
/** Content type for request. */
private static final String PROTOCOL_CONTENT_TYPE = "application/x-protobuf";
private static final Object sDecodeLock = new Object();
private RequestData mRequestData;
private BaseCallback mCallback;
public ProtobufRequest(RequestData data, BaseCallback callback) {
super(Method.POST, Constants.SERVER_URL, callback);
// TODO Auto-generated constructor stub
mRequestData = data;
mCallback = callback;
this.setRetryPolicy(getRetryPolicy());
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Charset", "UTF-8");
headers.put("Content-Type", "application/x-protobuf");
headers.put("Accept", "application/x-protobuf");
return headers;
}
#Override
public String getBodyContentType() {
return PROTOCOL_CONTENT_TYPE;
}
#Override
public RetryPolicy getRetryPolicy() {
RetryPolicy retryPolicy = new DefaultRetryPolicy(SOCKET_TIMEOUT,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
return retryPolicy;
}
/**
** write the protobuf data to http request,it is very important
*/
#Override
public byte[] getBody() throws AuthFailureError {
if (mRequestData == null) {
return super.getBody();
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
baos.write(mRequestData.toByteArray());
} catch (IOException e) {
return super.getBody();
}
return baos.toByteArray();
}
}
#Override
protected void deliverResponse(Object arg0) {
mCallback.onResponse(arg0);
}
/**
** parse the response result
*/
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
synchronized (sDecodeLock) {
try {
return doParse(response, HttpHeaderParser.parseCacheHeaders(response));
} catch (OutOfMemoryError e) {
return Response.error(new ParseError(e));
}
}
}
abstract protected Response<T> doParse(NetworkResponse response, Entry entry)
public interface BaseCallback extends Listener<Object>, ErrorListener {
}
From: cmcneil Added code in case link rot. There should be an official implementation by Volley or Protocol Buffers eventually so please update this when necessary.
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.RetryPolicy;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.protobuf.MessageLite;
import java.util.HashMap;
import java.util.Map;
/**
* Created by carson#convox.org on 5/10/15.
*/
public class ProtoBufRequest<ReqT extends MessageLite, RespT extends MessageLite> extends Request<RespT> {
private ReqT request;
private final Class<RespT> responseType;
private final Listener<RespT> listener;
private static final String PROTOCOL_CONTENT_TYPE = "application/x-protobuf";
private static final int SOCKET_TIMEOUT = 30000;
public ProtoBufRequest(int method, String url, ReqT data, Class<RespT> responseType,
Listener<RespT> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = listener;
this.request = data;
this.responseType = responseType;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Charset", "UTF-8");
headers.put("Content-Type", PROTOCOL_CONTENT_TYPE);
headers.put("Accept", PROTOCOL_CONTENT_TYPE);
return headers;
}
#Override
public byte[] getBody() throws AuthFailureError {
if (request == null) {
return super.getBody();
}
return request.toByteArray();
}
#Override
protected Response<RespT> parseNetworkResponse(NetworkResponse response) {
try {
if (responseType == null) {
throw new IllegalArgumentException("The response type was never provided.");
}
RespT responseInstance = responseType.newInstance();
return (Response<RespT>) Response.success(
responseInstance.newBuilderForType().mergeFrom(response.data).build(),
HttpHeaderParser.parseCacheHeaders(response));
} catch (Exception e) {
e.printStackTrace();
return Response.error(new ParseError(e));
}
}
#Override
public String getBodyContentType() {
return PROTOCOL_CONTENT_TYPE;
}
#Override
public RetryPolicy getRetryPolicy() {
RetryPolicy retryPolicy = new DefaultRetryPolicy(SOCKET_TIMEOUT,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
return retryPolicy;
}
#Override
protected void deliverResponse(RespT response) {
listener.onResponse(response);
}
}
This is not an example and I have not (yet) done it myself but here is how I would do it:
One way is to extend JsonObjectRequest and serialize and deserialize every protocol buffer as Json using:
Message someProto = SomeProto.getDefaultInstance();
String jsonFormat = JsonFormat.printToString(someProto);
Message.Builder builder = SomeProto.newBuilder();
String jsonFormat = _load json document from a source_;
JsonFormat.merge(jsonFormat, builder);
See: https://code.google.com/p/protobuf-java-format/
Otherwise, if you want to send the protocol buffer serialize to byte[] you could extend Request<T>. The you would override public byte[] getBody() for the request body/payload, and
protected Response<Message> parseNetworkResponse(NetworkResponse response) for the answer.
You would then use the following functions to serialize the protocol buffer.
byte[] toByteArray();
parseFrom(byte[] data);
If you have a solution do not forget to share.
Just sticking this here in case it's useful:
I searched around a lot for a generic way to do something similar to jeff wang's answer. I couldn't find anything so I wrote this.
Let me know if you have any way to get rid of the unchecked cast in there...
ProtobufRequest.java
public class ProtoBufRequest<ReqT extends Message, RespT extends Message> extends Request<RespT> {
private ReqT request;
private final Class<RespT> responseType;
private final Listener<RespT> listener;
private static final String PROTOCOL_CONTENT_TYPE = "application/x-protobuf";
private static final int SOCKET_TIMEOUT = 30000;
public ProtoBufRequest(int method, String url, ReqT data, Class<RespT> responseType,
Listener<RespT> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = listener;
this.request = data;
this.responseType = responseType;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Charset", "UTF-8");
headers.put("Content-Type", PROTOCOL_CONTENT_TYPE);
headers.put("Accept", PROTOCOL_CONTENT_TYPE);
return headers;
}
#Override
public byte[] getBody() throws AuthFailureError {
if (request == null) {
return super.getBody();
}
return request.toByteArray();
}
#Override
protected Response<RespT> parseNetworkResponse(NetworkResponse response) {
try {
if (responseType == null) {
throw new IllegalArgumentException("The response type was never provided.");
}
RespT responseInstance = responseType.newInstance();
return (Response<RespT>) Response.success(
responseInstance.newBuilderForType().mergeFrom(response.data).build(),
HttpHeaderParser.parseCacheHeaders(response));
} catch (Exception e) {
e.printStackTrace();
return Response.error(new ParseError(e));
}
}
#Override
public String getBodyContentType() {
return PROTOCOL_CONTENT_TYPE;
}
#Override
public RetryPolicy getRetryPolicy() {
RetryPolicy retryPolicy = new DefaultRetryPolicy(SOCKET_TIMEOUT,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
return retryPolicy;
}
#Override
protected void deliverResponse(RespT response) {
listener.onResponse(response);
}
}