POST Request Json file passing String and wait for the response Volley

后端 未结 1 1724
一整个雨季
一整个雨季 2020-12-07 05:49

I am new to Android and Volley, and I need your help. I need to post a String, have a json to response to it and, if it doesn\'t raise a bad request, start a new intent with

相关标签:
1条回答
  • 2020-12-07 06:28

    I have answered some questions that look like your issue, such as:

    Android: How to return async JSONObject from method using Volley?

    You should not wait for that boolean return value. Instead, you can try the following way (of course, you can replace JSONArray request by JSONObject or String request):

    VolleyResponseListener listener = new VolleyResponseListener() {
                @Override
                public void onError(String message) {
                    // do something...
                }
    
                @Override
                public void onResponse(Object response) {
                    // do something...
                }
            };
    
    makeJsonArrayRequest(context, Request.Method.POST, url, requestBody, listener);
    

    Body of makeJsonArrayRequest can be as the following:

        public void makeJsonArrayRequest(Context context, int method, String url, String requestBody, final VolleyResponseListener listener) {
            JSONObject jsonRequest = null;        
            try {
                ...
                if (requestBody != null) {
                    jsonRequest = new JSONObject(requestBody);
                }
                ...
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(method, url, jsonRequest, new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray jsonArray) {
                    listener.onResponse(jsonArray);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    listener.onError(error.toString());
                }
            });
    
            // Access the RequestQueue through singleton class.
            MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
        }
    

    VolleyResponseListener interface as the following:

    public interface VolleyResponseListener {
        void onError(String message);
    
        void onResponse(Object response);
    }
    

    For your comments below:

    first of all is: the "order" of the method, for example in my case, After pressing button, which method I have to call?

    Let's assume we are inside onCreate: You can create VolleyResponseListener listener first, then call verifyCredentials(..., listener); when pressing the button.

    And where I can call the intent?

    This will be called inside onResponse of the above VolleyResponseListener listener (of course, inside that you can check more conditions depend on your requirements)

    Second: I have to send a String but I want a jsonArrayRespond, there is a method to do this? Or it work only with 2 kind of parameter such string request/string sent and json request/json sent?

    According to Google's training documentation:

    • StringRequest: Specify a URL and receive a raw string in response.
    • ImageRequest: Specify a URL and receive an image in response.
    • JsonObjectRequest and JsonArrayRequest (both subclasses of JsonRequest): Specify a URL and get a JSON object or array (respectively) in response.

    And of course you can implement your own custom request types, for types that don't have out-of-the-box Volley support. Take a look at Implementing a Custom Request.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题