Ich versuche, API url = "API-Adresse" zu verbinden, die zwei Header-Typen application/json zum Reponcen in json und application/xml zum Reponcen in xml akzeptiert. Ich muss JSON mit JSON-Parametern treffen und die Antwort wird auch im JSON-Format sein. Bei Verwendung von Android Volley Post Request mit JsonObjectRequest, das Header mithilfe von getHeaders festlegt, wird eine Verbindung zum Server hergestellt, getParams zum Festlegen von Parametern funktioniert jedoch nicht.
RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Constants.BASE_URL + Constants.LOGIN, null, response,
response_error) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Map<String, String> getPostParams()
throws AuthFailureError {
// TODO Auto-generated method stub
Map<String, String> params = new HashMap<String, String>();
params.put("key", "value");
return params;
}
};
// implementation of listeners
Response.Listener<JSONObject> response = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
Log.e("responce", response.toString());
// msgResponse.setText(response.toString());
hideProgressDialog();
}
};
Response.ErrorListener response_error = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error responce", error.getMessage());
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
};
//get params never get called
//i also tried alternative to send params but does not works.
Map<String, String> params = new HashMap<String, String>();
params.put("key", "value");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Constants.BASE_URL + Constants.LOGIN, new JSONObject(params), response,
response_error) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
};
any type of help will be appretiated, Thanks in advance.
Volley ignoriert Ihre Content-Type
Einstellung. Wenn Sie den Inhaltstyp ändern möchten, können Sie die getBodyContentType
Methode überschreiben:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, url,
new JSONObject(params), response, response_error) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
}
warum ignoriert Volley Ihre Parameter? werfen Sie einen Blick auf meine andere Antwort .
Volley JsonObjectRequest Post Anfrage funktioniert nicht
Nehmen Sie die andere Antwort aus diesem Beitrag, die mit Folgendem beginnt: Sie können ein benutzerdefiniertes JSONObjectReuqest erstellen