0
我正在使用Volley進行我的Android應用程序的REST連接。Android在傳遞JSON的REST GET調用中使用Volley
我無法弄清楚在進行GET調用時如何在頭中傳遞複雜的JSON數據。
這是我CustomRequest:
public class CustomRequest extends Request<JSONObject>
{
private int mMethod;
private String mUrl;
private Map<String, String> mParams; //for post
private Map<String, String> mHeaders; //for get
private Listener<JSONObject> mListener;
private JSONObject mJson;
public String token = "";
public CustomRequest(int method, String url, Map<String, String> params,Map<String, String> headers,
Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.mMethod = method;
this.mUrl = url;
this.mParams = params;
this.mHeaders = headers;
this.mListener = reponseListener;
}
public CustomRequest(int method, String url, JSONObject json,
Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.mMethod = method;
this.mUrl = url;
this.mJson = json;
this.mListener = reponseListener;
}
@Override
public String getUrl() {
//we are not doing get this way
if(mMethod == Request.Method.GET) {
StringBuilder stringBuilder = new StringBuilder(mUrl);
Iterator<Map.Entry<String, String>> iterator = mParams.entrySet().iterator();
int i = 1;
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
if(i == 1) {
stringBuilder.append("?" + entry.getKey() + "=" + entry.getValue());
} else {
stringBuilder.append("&" + entry.getKey() + "=" + entry.getValue());
}
iterator.remove(); // avoids a ConcurrentModificationException
i++;
}
mUrl = stringBuilder.toString();
}
Log.d("debug",mUrl);
return mUrl;
}
@Override
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return mParams;
};
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
if(mMethod == Request.Method.GET)
{
/*
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("Cookie", "userSession=" + token);
return headers;
*/
return mHeaders;
}
return mParams;
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONObject response) {
// TODO Auto-generated method stub
mListener.onResponse(response);
}
@Override
public byte[] getBody()
{
String body = "";
if(mParams != null)
{
JSONObject jsonObject = new JSONObject(mParams);
body = jsonObject.toString();
}
else if(mJson != null)
{
body = mJson.toString();
}
try
{
return body.toString().getBytes("utf-8");
} catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
它正常工作與POST和GET,如果我只是把它傳遞一個平坦的HashMap,但該呼叫我需要多層次發送更復雜的JSON: 這是數據我需要在頭派:
"Cookie": "userSession=" + dataMgr.savedToken
「json」:
{
「sorting」: (optional)
{
「property」: <string>,
「direction」: <int> (0 = ascending, 1 = descending) (optional)
}
「paging」: (optional)
{
「number」: <int>, (optional)
「size」: <int> (optional)
},}
的問題是,這樣的功能:
@Override
public Map<String, String> getHeaders() throws AuthFailureError
返回一個簡單的地圖,我的JSON不能轉換爲一個簡單的地圖
順便說一句,我不能傳帶 公共字符串的getURL(`) 頭出於某種原因,我們的REST服務器忽略它,我有改爲使用getHeaders()函數。
通過我知道這是非常規的通過JSON像這樣在頭的方式,但這個是服務器的人堅持,他是我的老闆。 – user3561494