gpt4 book ai didi

android - 以 JSONObject 作为参数的 Volley JSONArrayRequest

转载 作者:太空狗 更新时间:2023-10-29 15:51:00 24 4
gpt4 key购买 nike

我需要使用 JSONObject 发出请求,如下所示:

{
'LangIDs': [1, 2],
'GenreIDs': [4],
'LowPrice': 0,
'HighPrice': 999,
'SearchTerms': [],
'Pagination': {
'PageNumber': 0,
'PageLength': 10
}
}

预期的响应是一个 JSONArray。使用Volley ,我无法使用 JSONObject 参数创建 JsonArrayRequest。

在我这样提出请求之前:

StringRequest jsonObjectRequest = new StringRequest(Request.Method.POST, url, new Response.Listener < String > () {
@Override
public void onResponse(String response) {
JSONObject jsonObject;
int id;
String bookName;
String Url;
try {
responseArray = new JSONArray(response);
} catch (JSONException e) {

}

for (int i = 0; i < response.length(); i++) {
try {
jsonObject = responseArray.getJSONObject(i);
id = jsonObject.getInt(Constants.KEY_ID);
bookName = jsonObject.getString(Constants.KEY_BOOKNAME);
Url = imgUrl + jsonObject.getString(Constants.KEY_IMG_URL);
books.add(new Book(id, bookName, Url));

} catch (JSONException e) {
e.printStackTrace();
}
}
mAdapter.updateGrid(books);
Log.v("Response", response);
}
}, new Response.ErrorListener() {@
Override
public void onErrorResponse(VolleyError error) {
Log.e("Error:", error.getMessage());
}
}) {@
Override
protected Map < String, String > getParams() throws AuthFailureError {
HashMap < String, String > params = new HashMap < > ();
String message = getArguments().getString(Constants.KEY_FILTER_VALUES);
Log.v("FilterMessage", message);
params.put(Constants.KEY_PAGENUMBER, String.valueOf(0));
params.put(Constants.KEY_PAGELENGTH, String.valueOf(10));
return params;
}
};

但是现在是包含JSONObject的JSONObject。

我现在如何使用 Volley 发出此请求?

最佳答案

我创建了一个接受 JSONObject 作为参数的自定义截击请求。

CustomJsonArrayRequest.java

 public class CustomJsonArrayRequest extends JsonRequest<JSONArray> {

/**
* Creates a new request.
* @param method the HTTP method to use
* @param url URL to fetch the JSON from
* @param jsonRequest A {@link JSONObject} to post with 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 CustomJsonArrayRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
errorListener);
}

@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}

如何使用?

JSONObject body = new JSONObject();
// Your code, e.g. body.put(key, value);
CustomJsonArrayRequest req = new CustomJsonArrayRequest(Request.Method.POST, url, body, success, error);

关于android - 以 JSONObject 作为参数的 Volley JSONArrayRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39235567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com