Created
December 2, 2013 00:42
-
-
Save ursimon/7743040 to your computer and use it in GitHub Desktop.
GsonRequest w/ support for headers and body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2013 Ognyan Bankov | |
* modified by Michal Ursiny for purposes of Fuerte Eshop adding headers and body support | |
* | |
* 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 com.your.package; | |
import android.annotation.SuppressLint; | |
import android.os.Build; | |
import android.util.Log; | |
import com.google.common.base.Charsets; | |
import com.google.gson.Gson; | |
import com.android.volley.AuthFailureError; | |
import com.android.volley.Response.ErrorListener; | |
import com.android.volley.Response.Listener; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class GsonRequestHeaders<T> extends GsonRequest<T> { | |
/** Charset for request. */ | |
private static final String PROTOCOL_CHARSET = "utf-8"; | |
/** Content type for request. */ | |
private static final String PROTOCOL_CONTENT_TYPE = | |
String.format("application/json; charset=%s", PROTOCOL_CHARSET); | |
private final Map<String, String> mHeaders; | |
private final String mBody; | |
public GsonRequestHeaders(int method, | |
String url, | |
Class<T> clazz, | |
Listener<T> listener, | |
ErrorListener errorListener, | |
Map<String, String> headers, | |
String body | |
) { | |
super(method, url, clazz, listener, errorListener); | |
this.mHeaders = headers; | |
this.mBody = body; | |
} | |
public GsonRequestHeaders(int method, | |
String url, | |
Class<T> clazz, | |
Listener<T> listener, | |
ErrorListener errorListener, | |
Gson gson, | |
Map<String, String> headers, | |
String body | |
) { | |
super(method, url, clazz, listener, errorListener, gson); | |
this.mHeaders = headers; | |
this.mBody = body; | |
} | |
@SuppressLint("NewApi") | |
@Override | |
public byte[] getBody() throws AuthFailureError { | |
// ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
// try { | |
// mBody. | |
// entity.writeTo(bos); | |
// } catch (IOException e) { | |
// VolleyLog.e("IOException writing to ByteArrayOutputStream"); | |
// } | |
if (Build.VERSION.SDK_INT < 9 ) { | |
return mBody.getBytes(); | |
} | |
else return mBody.getBytes(Charsets.UTF_8); | |
} | |
@Override | |
public String getBodyContentType() { | |
return PROTOCOL_CONTENT_TYPE; | |
} | |
/* (non-Javadoc) | |
* @see com.android.volley.Request#getHeaders() | |
*/ | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> headers = super.getHeaders(); | |
if (headers == null | |
|| headers.equals(Collections.emptyMap())) { | |
headers = new HashMap<String, String>(); | |
} | |
if (mHeaders!=null) { | |
headers.putAll(mHeaders); | |
} | |
Log.v("GsonRequestHeaders",headers.toString()); | |
return headers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment