Eu gostaria de enviar um novo JsonObjectRequest
pedido:
- Quero receber dados JSON (resposta do servidor): OK
Quero enviar dados formatados em JSON com esta solicitação para o servidor
JsonObjectRequest request = new JsonObjectRequest( Request.Method.POST, "myurl.com", null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { //... } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //... } }) { @Override protected Map<String,String> getParams() { // something to do here ?? return params; } @Override public Map<String, String> getHeaders() throws AuthFailureError { // something to do here ?? return params; } };
PS Eu uso a biblioteca GSON no meu projeto também.
android
http-post
android-volley
android-json
Antônio
fonte
fonte
HashMap
é meio redundante em seu exemplo. Você pode colocar o 'token' diretamente em umJSONObject
sem o mapa intermediário.new JSONObject("{\"type\":\"example\"}")
vez disso - que pena.Eu sei que este tópico é bastante antigo, mas eu tive esse problema e eu vim com uma solução legal que pode ser muito útil para muitos porque corrige / amplia a biblioteca do Volley em muitos aspectos.
Eu identifiquei alguns recursos não compatíveis do Volley:
JSONObjectRequest
não é perfeito: você tem que esperar umJSON
no final (veja oResponse.Listener<JSONObject>
).ResponseListener
?Eu mais ou menos compilei muitas soluções em uma grande classe genérica para ter uma solução para todo o problema que citei.
/** * Created by laurentmeyer on 25/07/15. */ public class GenericRequest<T> extends JsonRequest<T> { private final Gson gson = new Gson(); private final Class<T> clazz; private final Map<String, String> headers; // Used for request which do not return anything from the server private boolean muteRequest = false; /** * Basically, this is the constructor which is called by the others. * It allows you to send an object of type A to the server and expect a JSON representing a object of type B. * The problem with the #JsonObjectRequest is that you expect a JSON at the end. * We can do better than that, we can directly receive our POJO. * That's what this class does. * * @param method: HTTP Method * @param classtype: Classtype to parse the JSON coming from the server * @param url: url to be called * @param requestBody: The body being sent * @param listener: Listener of the request * @param errorListener: Error handler of the request * @param headers: Added headers */ private GenericRequest(int method, Class<T> classtype, String url, String requestBody, Response.Listener<T> listener, Response.ErrorListener errorListener, Map<String, String> headers) { super(method, url, requestBody, listener, errorListener); clazz = classtype; this.headers = headers; configureRequest(); } /** * Method to be called if you want to send some objects to your server via body in JSON of the request (with headers and not muted) * * @param method: HTTP Method * @param url: URL to be called * @param classtype: Classtype to parse the JSON returned from the server * @param toBeSent: Object which will be transformed in JSON via Gson and sent to the server * @param listener: Listener of the request * @param errorListener: Error handler of the request * @param headers: Added headers */ public GenericRequest(int method, String url, Class<T> classtype, Object toBeSent, Response.Listener<T> listener, Response.ErrorListener errorListener, Map<String, String> headers) { this(method, classtype, url, new Gson().toJson(toBeSent), listener, errorListener, headers); } /** * Method to be called if you want to send some objects to your server via body in JSON of the request (without header and not muted) * * @param method: HTTP Method * @param url: URL to be called * @param classtype: Classtype to parse the JSON returned from the server * @param toBeSent: Object which will be transformed in JSON via Gson and sent to the server * @param listener: Listener of the request * @param errorListener: Error handler of the request */ public GenericRequest(int method, String url, Class<T> classtype, Object toBeSent, Response.Listener<T> listener, Response.ErrorListener errorListener) { this(method, classtype, url, new Gson().toJson(toBeSent), listener, errorListener, new HashMap<String, String>()); } /** * Method to be called if you want to send something to the server but not with a JSON, just with a defined String (without header and not muted) * * @param method: HTTP Method * @param url: URL to be called * @param classtype: Classtype to parse the JSON returned from the server * @param requestBody: String to be sent to the server * @param listener: Listener of the request * @param errorListener: Error handler of the request */ public GenericRequest(int method, String url, Class<T> classtype, String requestBody, Response.Listener<T> listener, Response.ErrorListener errorListener) { this(method, classtype, url, requestBody, listener, errorListener, new HashMap<String, String>()); } /** * Method to be called if you want to GET something from the server and receive the POJO directly after the call (no JSON). (Without header) * * @param url: URL to be called * @param classtype: Classtype to parse the JSON returned from the server * @param listener: Listener of the request * @param errorListener: Error handler of the request */ public GenericRequest(String url, Class<T> classtype, Response.Listener<T> listener, Response.ErrorListener errorListener) { this(Request.Method.GET, url, classtype, "", listener, errorListener); } /** * Method to be called if you want to GET something from the server and receive the POJO directly after the call (no JSON). (With headers) * * @param url: URL to be called * @param classtype: Classtype to parse the JSON returned from the server * @param listener: Listener of the request * @param errorListener: Error handler of the request * @param headers: Added headers */ public GenericRequest(String url, Class<T> classtype, Response.Listener<T> listener, Response.ErrorListener errorListener, Map<String, String> headers) { this(Request.Method.GET, classtype, url, "", listener, errorListener, headers); } /** * Method to be called if you want to send some objects to your server via body in JSON of the request (with headers and muted) * * @param method: HTTP Method * @param url: URL to be called * @param classtype: Classtype to parse the JSON returned from the server * @param toBeSent: Object which will be transformed in JSON via Gson and sent to the server * @param listener: Listener of the request * @param errorListener: Error handler of the request * @param headers: Added headers * @param mute: Muted (put it to true, to make sense) */ public GenericRequest(int method, String url, Class<T> classtype, Object toBeSent, Response.Listener<T> listener, Response.ErrorListener errorListener, Map<String, String> headers, boolean mute) { this(method, classtype, url, new Gson().toJson(toBeSent), listener, errorListener, headers); this.muteRequest = mute; } /** * Method to be called if you want to send some objects to your server via body in JSON of the request (without header and muted) * * @param method: HTTP Method * @param url: URL to be called * @param classtype: Classtype to parse the JSON returned from the server * @param toBeSent: Object which will be transformed in JSON via Gson and sent to the server * @param listener: Listener of the request * @param errorListener: Error handler of the request * @param mute: Muted (put it to true, to make sense) */ public GenericRequest(int method, String url, Class<T> classtype, Object toBeSent, Response.Listener<T> listener, Response.ErrorListener errorListener, boolean mute) { this(method, classtype, url, new Gson().toJson(toBeSent), listener, errorListener, new HashMap<String, String>()); this.muteRequest = mute; } /** * Method to be called if you want to send something to the server but not with a JSON, just with a defined String (without header and not muted) * * @param method: HTTP Method * @param url: URL to be called * @param classtype: Classtype to parse the JSON returned from the server * @param requestBody: String to be sent to the server * @param listener: Listener of the request * @param errorListener: Error handler of the request * @param mute: Muted (put it to true, to make sense) */ public GenericRequest(int method, String url, Class<T> classtype, String requestBody, Response.Listener<T> listener, Response.ErrorListener errorListener, boolean mute) { this(method, classtype, url, requestBody, listener, errorListener, new HashMap<String, String>()); this.muteRequest = mute; } @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { // The magic of the mute request happens here if (muteRequest) { if (response.statusCode >= 200 && response.statusCode <= 299) { // If the status is correct, we return a success but with a null object, because the server didn't return anything return Response.success(null, HttpHeaderParser.parseCacheHeaders(response)); } } else { try { // If it's not muted; we just need to create our POJO from the returned JSON and handle correctly the errors String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); T parsedObject = gson.fromJson(json, clazz); return Response.success(parsedObject, HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JsonSyntaxException e) { return Response.error(new ParseError(e)); } } return null; } @Override public Map<String, String> getHeaders() throws AuthFailureError { return headers != null ? headers : super.getHeaders(); } private void configureRequest() { // Set retry policy // Add headers, for auth for example // ... } }
Pode parecer um pouco exagerado, mas é muito legal ter todos esses construtores porque você tem todos os casos:
(O construtor principal não foi feito para ser usado diretamente, embora seja, obviamente, possível).
Claro, para que funcione, você precisa ter o GSON Lib do Google; basta adicionar:
compile 'com.google.code.gson:gson:x.y.z'
às suas dependências (a versão atual é
2.3.1
).fonte
toBeSent
parâmetros deObject
paraT
para obter mais segurança de tipo.final String URL = "/volley/resource/12"; // Post params to be sent to the server HashMap<String, String> params = new HashMap<String, String>(); params.put("token", "AbCdEfGh123456"); JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params), new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { VolleyLog.v("Response:%n %s", response.toString(4)); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.e("Error: ", error.getMessage()); } }); // add the request object to the queue to be executed ApplicationController.getInstance().addToRequestQueue(req);
referir
fonte
Crie um objeto de
RequestQueue
classe.RequestQueue queue = Volley.newRequestQueue(this);
Crie um
StringRequest
listener com resposta e erro.StringRequest sr = new StringRequest(Request.Method.POST,"http://api.someservice.com/post/comment", new Response.Listener<String>() { @Override public void onResponse(String response) { mPostCommentResponse.requestCompleted(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { mPostCommentResponse.requestEndedWithError(error); } }){ @Override protected Map<String,String> getParams(){ Map<String,String> params = new HashMap<String, String>(); params.put("user",userAccount.getUsername()); params.put("pass",userAccount.getPassword()); params.put("comment", Uri.encode(comment)); params.put("comment_post_ID",String.valueOf(postId)); params.put("blogId",String.valueOf(blogId)); return params; } @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String,String> params = new HashMap<String, String>(); params.put("Content-Type","application/x-www-form-urlencoded"); return params; } };
Adicione sua solicitação ao
RequestQueue
.queue.add(jsObjRequest);
Crie uma
PostCommentResponseListener
interface apenas para que você possa vê-la. É um delegado simples para a solicitação assíncrona.public interface PostCommentResponseListener { public void requestStarted(); public void requestCompleted(); public void requestEndedWithError(VolleyError error); }
Inclui permissão de INTERNET dentro do
AndroidManifest.xml
arquivo.<uses-permission android:name="android.permission.INTERNET"/>
fonte
final String url = "some/url";
ao invés de:
final JSONObject jsonBody = "{\"type\":\"example\"}";
você pode usar:
JSONObject jsonBody = new JSONObject(); try { jsonBody.put("type", "my type"); } catch (JSONException e) { e.printStackTrace(); } new JsonObjectRequest(url, jsonBody, new Response.Listener<JSONObject>() { ... });
fonte
final Map<String,String> params = new HashMap<String,String>(); params.put("email", customer.getEmail()); params.put("password", customer.getPassword()); String url = Constants.BASE_URL+"login"; doWebRequestPost(url, params); public void doWebRequestPost(String url, final Map<String,String> json){ getmDialogListener().showDialog(); StringRequest post = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { try { getmDialogListener().dismissDialog(); response.... } catch (Exception e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d(App.TAG,error.toString()); getmDialogListener().dismissDialog(); } }){ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> map = json; return map; } }; App.getInstance().getRequestQueue().add(post); }
fonte
Você também pode enviar dados substituindo o
getBody()
método daJsonObjectRequest
classe. Como mostrado abaixo.@Override public byte[] getBody() { JSONObject jsonObject = new JSONObject(); String body = null; try { jsonObject.put("username", "user123"); jsonObject.put("password", "Pass123"); body = jsonObject.toString(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { return body.toString().getBytes("utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
fonte
protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); JSONObject JObj = new JSONObject(); try { JObj.put("Id","1"); JObj.put("Name", "abc"); } catch (Exception e) { e.printStackTrace(); } params.put("params", JObj.toString()); // Map.Entry<String,String> Log.d("Parameter", params.toString()); return params; }
fonte