我想使用排球库从API获取数据,但我不知道如何使用排球请求发送参数。我无法确定是使用发布请求还是获取请求或下达请求。 我使用以下代码进行截击请求
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
public static void put(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.put(getAbsoluteUrl(url), params, responseHandler);
}
这是我的整个代码:
private void getCalendarEvents(String StartDate,String EndDate,String UserId,String CityID,String IsAdmin) {
RequestParams requestParams = new RequestParams();
requestParams.put("StartDate", StartDate);
requestParams.put("EndDate", EndDate);
requestParams.put("UserId", UserId);
requestParams.put("CityID", CityID);
requestParams.put("IsAdmin", IsAdmin);
RelayRestClient.put("CalendarGetEvents", requestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if (responseBody != null)
Log.e("success", new String(responseBody));
parse(new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.e("failure", new String(responseBody));
}
});
}
protected void parse(String s) {
try {
JSONObject mainObject = new JSONObject(s);
if (mainObject.has("Status")) {
String Status = mainObject.getString("Status");
if (Status.equalsIgnoreCase("200")) {
if (mainObject.has("Data")) {
JSONArray datArray = mainObject.getJSONArray("Data");
eventBeanArrayList = new ArrayList<>();
if (datArray.length() > 0) {
for (int i = 0; i < datArray.length(); i++) {
EventsBean eventsBean = new EventsBean();
JSONObject internalDataObject = datArray.getJSONObject(i);
if (internalDataObject.has("UserID")) {
eventsBean.setUserID(internalDataObject.getString("UserID"));
}
if (internalDataObject.has("EventID")) {
eventsBean.setEventID(internalDataObject.getString("EventID"));
}
if (internalDataObject.has("CityID")) {
eventsBean.setCityID(internalDataObject.getString("CityID"));
}
if (internalDataObject.has("Title")) {
eventsBean.setTitle(internalDataObject.getString("Title"));
}
eventBeanArrayList.add(eventsBean);
}
setViewEventData(eventBeanArrayList);
} else {
showMessage("No Events found for this date.");
}
}
} else {
showMessage("Server error, Please try again");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void setViewEventData(ArrayList<EventsBean> eventBeanArrayList) {
lv_events.setAdapter(new ListAdapter(ViewEventActivity.this,eventBeanArrayList));
}
答案 0 :(得分:1)
您可以通过覆盖您的截击请求的getParams方法来使用HashMaps和post参数。看起来应该是这样的:
StringRequest sr = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Handle Response
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Handle volley Error
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<>();
params.put(key1,value1);
params.put(key2,value2);
params.put(key3,value3);
params.put(key4,value4);
params.put(key5,value5);
return params;
}
};