我是Android的初学者,我遇到了麻烦。如何使用API解析和发布任何内容。
我有两个API:
第二个API工作正常并给出响应,但是当我使用第一个API时,它不会产生响应,但Postman上的两个API都是相同的。我不知道我的代码有什么问题。
我想用第一个API替换第二个API。
这是我的代码:
private void getEventTypeList(){
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
// JSONObject jsonObject = null;
JSONObject jsonObject = new JSONObject();
CustomJSONObjectRequest jsonObjectRequest = new CustomJSONObjectRequest(Request.Method.POST, Constants.EVENT_TYPE_LIST_URL, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Utility.showLogError(PostRequirParty.this, "GetEventTypeListResponse" + response.toString());
DataBean bean = new DataBean();
bean = parser.getEventTypeList(response);
eventBeansList = new ArrayList<EventTypeBean>();
eventBeansList = bean.getEventTypeList();
eventNamesArr = new String[eventBeansList.size()];
for (int i = 0; i < eventBeansList.size(); i++) {
eventNamesArr[i] = eventBeansList.get(i).getEvent_type_name();
}
// hiding progressDialog
pDialog.hide();
ArrayAdapter<String> eventTypeAdapter = new ArrayAdapter<String>(PostRequirParty.this, R.layout.spinner_item, eventNamesArr);
eventTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
event.setAdapter(new NothingSelectedSpinnerAdapter(eventTypeAdapter, R.layout.spinner_row_nothing_selected, PostRequirParty.this));
//event.setPrompt("Select");
event.setAdapter(eventTypeAdapter);
final DataBean finalBean = bean;
event.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
event_id = finalBean.getEventTypeList().get(position).getEvent_type_id();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.hide();
Utility.showToastMsg(PostRequirParty.this, error.getMessage());
}
});
Utility.showLogError(PostRequirParty.this, "GetEventTypeList URL = " + Constants.EVENT_TYPE_LIST_URL);
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
}
这是我的解析代码:
public DataBean getEventTypeList(JSONObject response) {
DataBean dataBean = new DataBean();
ArrayList<EventTypeBean> eventTypeList = new ArrayList<EventTypeBean>();
if (response != null){
try{
JSONArray jsonArray = response.getJSONArray(Constants.DATA);
for (int i = 0; i < jsonArray.length(); i++) {
EventTypeBean typeBean = new EventTypeBean();
typeBean.setEvent_type_id(jsonArray.getJSONObject(i).getString(Constants.EVENT_TYPE_ID));
typeBean.setEvent_type_name(jsonArray.getJSONObject(i).getString(Constants.EVENT_TYPE_NAME));
eventTypeList.add(typeBean);
}
}catch (JSONException e){
e.printStackTrace();
}
dataBean.setEventTypeList(eventTypeList);
}
return dataBean;
}