Tuesday 23 July 2013

Volley in android

Volley is a library and also powerful tool that makes networking for Android apps easier and most importantly, faster.It's an alternative of AsyncTask.
It manages the processing and caching of network requests and it saves developers valuable time from writing the same network call/cache code again and again.You can easily load thumbnail images for your ListView from the network in parallel.
Clone the volley project from

 https://github.com/adamrocker/volley

Advantages:
    Volley automatically schedule all network requests. It means that Volley will be taking care of all the network requests your app executes for fetching response or image from web.
    Volley provides transparent disk and memory caching.
    Volley provides powerful cancellation request API. It means that you can cancel a single request or you can set blocks or scopes of requests to cancel.
    Volley provides powerful customization abilities.
    Volley provides Debugging and tracing tools

There are 2 main classes:
1. Request queue
2. Request



private void getServices(String url) {
//        JSONObject reqBody = new JSONObject();
        Map<String,String> params = new HashMap<String,String>();
        if (url.equals(URL_SUCCESS)) {
            try {
                params.put("id", "Demo");
                params.put("output", "json");
                url = addParamsToUrl(url, params);
                volleyRequestQueue = Volley.newRequestQueue(this);
                volleyRequestQueue.add(new JsonObjectRequest(Method.GET, url, null, new MyResponseListener(), new MyErrorListener()));
              
              
            } catch (Exception e) {
                Log.e("ErrorListener", e.getLocalizedMessage());
            }
        } else {
            params.put("level", "1");
            url = addParamsToUrl(url, params);
            volleyRequestQueue = Volley.newRequestQueue(this);
            volleyRequestQueue.add(new JsonObjectRequest(Method.GET, url, null, new MyResponseListener(), new MyErrorListener()));
        }
    }

class MyResponseListener implements Listener<JSONObject> {

        @Override
        public void onResponse(JSONObject response) {
            MainActivity.this.jsonObj = response;
            try {
                JSONObject result = MainActivity.this.jsonObj.getJSONObject("Result");
                String timestamp = result.getString("Timestamp");
                MainActivity.this.resultTxt.setText(new Date(Long.parseLong(timestamp.trim())).toString());
            } catch (Exception e) {
                Log.e("ErrorListener", e.getLocalizedMessage());
            }
        }
    }


and the class error.


class MyErrorListener implements ErrorListener {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("ErrorListener", error.getCause() + "");
            if (error.getCause() != null) {
                Log.e("ErrorListener", error.getLocalizedMessage());
            }
            MainActivity.this.resultTxt.setText(error.toString());
            volleyRequestQueue.cancelAll(this);
        }
    }