GITcommitEd
GITcommitEd

Reputation: 293

Volley Request Manager

I'm using Volley however I'm having some problems with the JSON parsed data most likely because volley doesn't implement something like AsyncTask's onPostExecute() and I'm getting some duplicated data on wrong list items.

Then I came across this: https://github.com/yakivmospan/volley-request-manager#custom-listener-implementation-

Has anyone use it? How can I add it to my current Volley code?

More details about my problem here Volley not sending correct data. How to implement an alternative to onPostExecute()?

UPDATE

As requested, some code. Here's a button that calls a method on another class that uses Volley to request some raw JSON data (NovaJSON) and then send the JSON to a parser class (NovaParser):

    info.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String instanceDetail = NovaJSON.shared().receiveDetail(getId());
            Dialog dialog = new Dialog(v.getContext());
            dialog.setContentView(R.layout.instances_info);
            TextView image = (TextView) dialog.findViewById(R.id.imageInstance);
            TextView flavor = (TextView) dialog.findViewById(R.id.flavorInstance);
            dialog.setTitle(name.getText() + " Details");
            if (instanceDetail != null) {
                image.setText(" \u2022 image : " + NovaParser.shared().parseImages(instanceDetail));
                flavor.setText(" \u2022 flavor : " + NovaParser.shared().parseFlavor(instanceDetail));
            }
            dialog.show();
        }
    });

This is the method that does the Volley request on the NovaJSON class:

    public void getJSONdetail() {
    final String authToken = getAuth();
    String novaURL = getNova();
    novaURL = novaURL+"/servers/"+id;


    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, novaURL, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("Nova on Response", response.toString());
                    setNovaJSONdetail(response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("Nova on Error", "Error: " + error.getMessage());
                    setNovaJSONdetail(error.toString());
                }
            }
    ) {
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("X-Auth-Token", authToken);
            params.put("User-Agent", "stackerz");
            params.put("Accept", "application/json");
            params.put("Content-Type", "application/json; charset=utf-8");
            return params;
        }

    };


    queue = VolleySingleton.getInstance(this).getRequestQueue();
    queue.add(getRequest);
}

It then sends the JSON from the server as a string to be parsed using the following methods:

public static String parseImages(String imagesDetail){
    ArrayList<HashMap<String, String>> imagesList = NovaParser.shared().getImagesList();
    String temp = null;
    JSONObject novaDetail = null;
    try {
        novaDetail = new JSONObject(imagesDetail);
        JSONObject server = novaDetail.getJSONObject("server");
        JSONObject image = server.getJSONObject("image");
        if (imagesList !=null){
            temp = image.getString("id");
            for (Map<String,String> map : imagesList) {
                if (map.containsValue(temp)) {
                    temp = map.get(NAME);
                }
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return temp;
}

public static String parseFlavor(String instanceDetail){
    ArrayList<HashMap<String, String>> flavorList = NovaParser.shared().getFlavorList();
    String temp = null;
    JSONObject novaDetail = null;
    try {
        novaDetail = new JSONObject(instanceDetail);
        JSONObject server = novaDetail.getJSONObject("server");
        JSONObject flavor = server.getJSONObject("flavor");
        if (flavorList !=null){
        temp = flavor.getString("id");
        for (Map<String,String> map : flavorList) {
            if (map.containsValue(temp)) {
                temp = map.get(NAME);
            }
        }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return temp;
}

When I press the button once the dialog is displayed with empty values. When I press it the second time I get the correct parsed data. Basically first time I click the button the instanceDetail string is null because Volley didn't finish doing its thing then I click the 2nd time it loads the values accordingly because it finally finished the 1st request.

I understand Volley is asynchronous, the requests happen in parallel and the responses sometimes are not immediate however I need some sort of progress bar or spinning wheel to give the user some feedback that the app is waiting for data. It could be done with AsyncTask however it doesn't seem to be possible with Volley.

Upvotes: 0

Views: 1138

Answers (2)

GITcommitEd
GITcommitEd

Reputation: 293

I solved my problem by dumping Volley altogether and moving to Retrofit. I setup all the calls to be sync/blocking, worked out the exceptions/errors using try/catches and setup a short timeout on the OkHTTP client. Now it's working as I wanted.

Upvotes: 0

NickF
NickF

Reputation: 5737

I think your problem is not because of Volley.
Check the parameters you send and receive.
However if you need onPostExcecute you have Volley's callback:
Response.Listener<JSONObject> and Response.ErrorListener() which are called after the request.

About Volley request manager just switch all your volley calls with appropriate Volley request manager calls

Upvotes: 0

Related Questions