Skip to content

Instantly share code, notes, and snippets.

@w4lle
Created June 9, 2015 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w4lle/be02fa087a6635a226ed to your computer and use it in GitHub Desktop.
Save w4lle/be02fa087a6635a226ed to your computer and use it in GitHub Desktop.
volley get 方法自动添加params
/**
* Created by w4lle on 15-4-14.
* Copyright (c) 2015 Boohee, Inc. All rights reserved.
*/
package com.thousandlotus.care.volley;
import android.text.TextUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.AuthFailureError;
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.thousandlotus.care.BuildConfig;
import com.thousandlotus.care.util.AccountUtils;
import com.thousandlotus.care.util.AppUtils;
import com.thousandlotus.care.util.SystemUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* 返回结果替换为fastJson
*/
public class FastJsonRequest extends JsonRequest<JSONObject> {
protected static final boolean DEBUG = BuildConfig.API_DEBUG;
public static final String PRODUCTION = "http://care.boohee.com";
public static final String QA = "http://care.ejianfei.com";
public static String getURL(String url) {
if (DEBUG) {
return QA + url;
}
return PRODUCTION + url;
}
public FastJsonRequest(String url, JsonParams jsonParams, JsonCallback callback) {
this(jsonParams == null ? Method.GET : Method.POST, url, jsonParams, callback);
}
public FastJsonRequest(int method, String url, JsonParams jsonParams, JsonCallback callback) {
super(method, handleUrl(method, url, jsonParams), jsonParams == null ? null : jsonParams.toString(), callback, callback);
}
/**
* get方式拼接url
* @param method
* @param url
* @param jsonParams
* @return
*/
public static String handleUrl(int method, String url, JsonParams jsonParams) {
String mUrl = null;
if (method == Method.GET) {
mUrl = getURL(url);
if (jsonParams != null) {
try {
StringBuilder stringBuilder = new StringBuilder(mUrl);
JSONObject jsonObject = jsonParams.toJson();
org.json.JSONObject tmpObject = new org.json.JSONObject(jsonObject.toJSONString());
Iterator<?> iterator = tmpObject.keys();
List<NameValuePair> params = new ArrayList<NameValuePair>();
while (iterator.hasNext()) {
String key = (String) iterator.next();
String value = jsonObject.getString(key);
if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
params.add(new BasicNameValuePair(key ,value));
}
}
if (params != null && params.size() > 0) {
stringBuilder.append("?");
stringBuilder.append(URLEncodedUtils.format(params, HTTP.UTF_8));
mUrl = stringBuilder.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}else {
mUrl = getURL(url);
}
return mUrl;
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString =
new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(JSON.parseObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("app_version", AppUtils.getVersionName());
headers.put("app_key", "care"); // App代号
headers.put("app_device", "Android"); // App终端
headers.put("os_version", SystemUtils.getVersionCode()); // 系统版本
headers.put("phone_model", SystemUtils.getPhoneModel()); // 手机型号
headers.put("Accept", "application/json");
headers.put("User-Agent", "Android/Volley");
String token = AccountUtils.getToken();
if (!TextUtils.isEmpty(token)) {
headers.put("token", token);// 认证信息
}
return headers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment