Created
July 17, 2015 08:56
-
-
Save garymabin/8a031ce84496c69b18b1 to your computer and use it in GitHub Desktop.
BasicJSONRequest
This file contains hidden or 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
package com.brd.igoshow.core.request; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLEncoder; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import android.content.Context; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager.NameNotFoundException; | |
import android.os.Bundle; | |
import android.os.Message; | |
import android.util.Log; | |
import com.android.volley.AuthFailureError; | |
import com.android.volley.NetworkResponse; | |
import com.android.volley.Response; | |
import com.android.volley.Response.ErrorListener; | |
import com.android.volley.Response.Listener; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.HttpHeaderParser; | |
import com.android.volley.toolbox.JsonRequest; | |
import com.brd.igoshow.StaticApplication; | |
import com.brd.igoshow.common.ParcelablePoolObject; | |
import com.brd.igoshow.model.IResourceConstants; | |
public abstract class BasicJSONRequest<T> extends JsonRequest<T> implements | |
IResourceConstants { | |
/** 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/x-www-form-urlencoded; charset=%s", PROTOCOL_CHARSET); | |
private static final String TAG = "BasicJSONRequest"; | |
protected Message mParam; | |
protected String mGetUrl; | |
public BasicJSONRequest(int method, String url, Listener<T> listener, | |
ErrorListener errorListener, Message param) { | |
super(method, url, "", listener, errorListener); | |
mParam = param; | |
} | |
public BasicJSONRequest(String url, Listener<T> listener, | |
ErrorListener errorListener, Message param) { | |
super(Method.POST, url, "", listener, errorListener); | |
mParam = param; | |
} | |
@Override | |
public String getUrl() { | |
if (getMethod() == Method.POST) { | |
return super.getUrl(); | |
} else { | |
if (mGetUrl == null) { | |
mGetUrl = formatGetUrl(super.getUrl()); | |
} | |
return mGetUrl; | |
} | |
} | |
private String formatGetUrl(String orig) { | |
StringBuilder sb = new StringBuilder(orig); | |
Map<String, String> params = getParamsFromBundle(((ParcelablePoolObject) mParam.obj) | |
.getData()); | |
if (params != null && params.size() > 0) { | |
sb.append("?"); | |
try { | |
for (Map.Entry<String, String> entry : params.entrySet()) { | |
sb.append(URLEncoder.encode(entry.getKey(), PROTOCOL_CHARSET)); | |
sb.append('='); | |
sb.append(URLEncoder.encode(entry.getValue(), | |
PROTOCOL_CHARSET)); | |
sb.append('&'); | |
} | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
} | |
return sb.toString(); | |
} | |
@Override | |
protected Response<T> parseNetworkResponse(NetworkResponse response) { | |
String error = "unknown error"; | |
try { | |
String jsonString = new String(response.data, | |
HttpHeaderParser.parseCharset(response.headers)); | |
Log.d(TAG, "receive json result: " + jsonString); | |
JSONObject obj = new JSONObject(jsonString); | |
if (obj.has(JSON_KEY_COMMON_CODE)) { | |
if (obj.getInt(JSON_KEY_COMMON_CODE) >= COMMON_SUCCESS) { | |
return Response.success(parseResult(obj), getCacheEntry()); | |
} else { | |
if (obj.has(JSON_KEY_COMMON_MSG)) { | |
error = obj.getString(JSON_KEY_COMMON_MSG); | |
} else { | |
error = obj.getInt(JSON_KEY_COMMON_CODE) + ""; | |
} | |
} | |
} | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
error = "unsupported encoding"; | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
error = "json parsed error"; | |
} | |
return Response.error(new VolleyError(error)); | |
} | |
protected abstract T parseResult(JSONObject ob); | |
protected abstract Map<String, String> getParamsFromBundle(Bundle param); | |
@Override | |
public byte[] getBody() { | |
Map<String, String> params = getParamsFromBundle(((ParcelablePoolObject) mParam.obj) | |
.getData()); | |
if (params != null && params.size() > 0) { | |
return encodeParameters(params, getParamsEncoding()); | |
} | |
return null; | |
} | |
@Override | |
public String getBodyContentType() { | |
return PROTOCOL_CONTENT_TYPE; | |
} | |
/** | |
* Converts <code>params</code> into an application/x-www-form-urlencoded | |
* encoded string. | |
*/ | |
private byte[] encodeParameters(Map<String, String> params, | |
String paramsEncoding) { | |
StringBuilder encodedParams = new StringBuilder(); | |
try { | |
for (Map.Entry<String, String> entry : params.entrySet()) { | |
encodedParams.append(URLEncoder.encode(entry.getKey(), | |
paramsEncoding)); | |
encodedParams.append('='); | |
encodedParams.append(URLEncoder.encode(entry.getValue(), | |
paramsEncoding)); | |
encodedParams.append('&'); | |
} | |
return encodedParams.toString().getBytes(paramsEncoding); | |
} catch (UnsupportedEncodingException uee) { | |
throw new RuntimeException("Encoding not supported: " | |
+ paramsEncoding, uee); | |
} | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> headers = new HashMap<String, String>(); | |
Context context = StaticApplication.peekInstance(); | |
if (context != null) { | |
StringBuilder label = new StringBuilder(); | |
label.append("IgoShowAndroid_V"); | |
try { | |
PackageInfo pInfo = context.getPackageManager().getPackageInfo( | |
context.getPackageName(), 0); | |
label.append(pInfo.versionName); | |
} catch (NameNotFoundException e) { | |
} | |
label.append(" "); | |
label.append(System.getProperty("http.agent")); | |
headers.put("User-Agent", label.toString()); | |
} | |
return headers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment