Hello All:
Developing a stable and professional application requires discipline and better coding practices. Sometimes it turns out that re-inventing the wheel also leads to some issues which can be avoided by using external libraries.
In this post I will share SEVEN must have libraries for any android application to enable fast and stable application development.
VolleyManager.Java
Downloading and caching images
Glide: This library is designed for high performance during scrolling. It is ideal for caching and showing data is scrollable layouts (Gridview, ListView, RecyclerView etc). More info can be found here
Using Glide is as simple as following code.
JSON serialization and de-serialization
GSON: This library provides easy to use methods for serializing and de-serializing data. More info can be found here.
Following is a helper class which can be used to serialize and de-serialize data.
Exception and Crash logging
Crashlytics: This library provides exception and crash logging which can help in catching bugs which may skip to production application. More info can be found here. Sample code can be found in this post.
Avoiding boiler plate code by Dependency Injection
ButterKnife: This library helps in removing boiler plate code which is repeated everywhere to nice and more readable code. More info can be found here. This library converts following code
TO
Passing data between activities
Parceler: This library makes it super easy to pass object between activities. More info can be found here.
Communicating and passing data as events
EventBus: This tiny library makes is very easy to pass data as events in the application. More info related to this library can be found here.
Developing a stable and professional application requires discipline and better coding practices. Sometimes it turns out that re-inventing the wheel also leads to some issues which can be avoided by using external libraries.
In this post I will share SEVEN must have libraries for any android application to enable fast and stable application development.
public class HttpRequest { ProgressDialog progressDialog; int responseCode; IAsyncCallback callback; Response.Listener<JSONObject> jsonResponseListener; Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { dismissProgressDialog(); callback.onError(new ErrorDetail(error)); } }; private HashMap<String, String> params; private HashMap<String, String> headers; private String url; private Activity context; private String gameObject; public HttpRequest(Activity localActivity, String url, String gameObjectName) { context = localActivity; this.url = url; gameObject = gameObjectName; headers = new HashMap<>(); params = new HashMap<>(); addJSONHeaders(); setListeners(); } public IAsyncCallback getCallback() { return callback; } public Context getContext() { return context; } private void addJSONHeaders() { AddHeader("Accept", "application/json"); AddHeader("Content-type", "application/json"); } private void setListeners() { jsonResponseListener = new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { dismissProgressDialog(); WebResponse backendResponse; try { String responseString = response.toString(); backendResponse = new WebResponse(responseCode, responseString); if (responseString == null) { callback.onError(new ErrorDetail(new Exception("Empty response returned"))); } else { callback.onComplete(backendResponse); } } catch (Exception exception) { exception.printStackTrace(); callback.onError(new ErrorDetail(exception)); } } }; } public void AddParam(String name, String value) { params.put(name, value); } public void AddHeader(String name, String value) { headers.put(name, value); } public void dismissProgressDialog() { context.runOnUiThread(new Runnable() { @Override public void run() { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } } }); } public void execute() { this.callback = new IAsyncCallback() { @Override public void onComplete(WebResponse responseContent) { //Do processing } @Override public void onError(ErrorDetail errorData) { //Do processing } }; context.runOnUiThread(new Runnable() { @Override public void run() { progressDialog = new ProgressDialog(getContext()); progressDialog.setCancelable(false); progressDialog.setMessage("Please wait..."); dismissProgressDialog(); progressDialog.show(); } }); addToRequestQueue(VolleyManager.getInstance(context).getRequestQueue(), getJsonRequest()); } public <X> void addToRequestQueue(RequestQueue requestQueue, Request<X> req) { requestQueue.add(req); } Request<JSONObject> getJsonRequest() { try { getUrl(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return new JsonObjectRequest(Request.Method.GET, url, jsonResponseListener, errorListener) { @Override public Map<String, String> getHeaders() throws AuthFailureError { return headers; } @Override protected Map<String, String> getParams() { return params; } }; } void getUrl() throws UnsupportedEncodingException { String combinedParams = ""; if (!params.isEmpty()) { combinedParams += "?"; for (Map.Entry<String, String> entry : params.entrySet()) { String paramString = entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"); if (combinedParams.length() > 1) { combinedParams += "&" + paramString; } else { combinedParams += paramString; } } } url = url + combinedParams; } }
VolleyManager.Java
public class VolleyManager { private static VolleyManager instance; private RequestQueue mRequestQueue; public RequestQueue getRequestQueue() { return mRequestQueue; } public synchronized static VolleyManager getInstance(Activity activity) { if (instance == null) { instance = new VolleyManager(); instance.mRequestQueue = Volley.newRequestQueue(activity); } return instance; } }
Downloading and caching images
Glide: This library is designed for high performance during scrolling. It is ideal for caching and showing data is scrollable layouts (Gridview, ListView, RecyclerView etc). More info can be found here
Using Glide is as simple as following code.
Glide.with(this).load(data.imageUrl).into(imageView);
JSON serialization and de-serialization
GSON: This library provides easy to use methods for serializing and de-serializing data. More info can be found here.
Following is a helper class which can be used to serialize and de-serialize data.
public class Json { public static <T> String serialize(T obj) { Gson gson = new Gson(); return gson.toJson(obj); } public static <T> T deSerialize(String jsonString, Class<T> tClass) throws ClassNotFoundException { Gson gson = new GsonBuilder().create(); return gson.fromJson(jsonString, tClass); } }
Exception and Crash logging
Crashlytics: This library provides exception and crash logging which can help in catching bugs which may skip to production application. More info can be found here. Sample code can be found in this post.
Avoiding boiler plate code by Dependency Injection
ButterKnife: This library helps in removing boiler plate code which is repeated everywhere to nice and more readable code. More info can be found here. This library converts following code
public class SampleActivity extends Activity { TextView txtName; TextView txtEmail; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.user); txtName = (TextView)findViewById(R.id.name); txtEmail = (TextView)findViewById(R.id.email); } }
TO
public class SampleActivity extends Activity { @Bind(R.id.name) TextView txtName; @Bind(R.id.email) TextView txtEmail; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.user); ButterKnife.bind(this); } }
Passing data between activities
Parceler: This library makes it super easy to pass object between activities. More info can be found here.
Communicating and passing data as events
EventBus: This tiny library makes is very easy to pass data as events in the application. More info related to this library can be found here.
- Define events:
public class SampleEvent { }
- Prepare subscribers:
eventBus.register(this); public void onEvent(SampleEvent event) {/* Do something */};
- Post events:
eventBus.post(event);
Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.
Keep visiting and sharing.
Thanks,
Ashwani.
0 comments :
Post a Comment