📜 ⬆️ ⬇️

Android Data Processor library for easy building of REST requests and their processing

In my daily work I constantly come across the development of applications using REST services. Existing libraries that help in building queries and processing them were not very suitable for a number of reasons. It was thought to create a simple tool like the Universal Image Loader that allows you to quickly build queries and parse the data. As a result, Android Data Processor appeared.

The data processor is designed to perform REST requests to services or locally to files.
Requests can be executed synchronously or asynchronously. The processor does not contain parsers. To process the results, you use your favorite data parsers and pass them the received data in the form of an InputStream, String.

CPU Initialization

To use the processor, it is necessary to initialize it using the configurator. The configurator allows you to set the basic parameters of the request according to http://developer.android.com/reference/java/net/URL.html , encoding, timeout, etc. This data is basic and can easily be modified when building specific queries. Initialization is most conveniently performed in the class inherited from Application:
')
private void initDataProcessor() { DataProcessorConfiguration configuration = DataProcessorConfiguration .getBuilder() .setHost("google.com") .setLogEnabled(true) .setShowProcessingTime(true) .setTimeout(4000) .build(); DataProcessor.getInstance().init(configuration); } 

Requests

At the moment, it is possible to construct GET, POST, MultipartRequest as well as processing local files.

Create request

 Request request = GetRequest.newInstance() .setLogTag("FB Login to server") .addGetParam("signature", "DH$FHJDDBHJV3393n") .setPath("login.php") .build(); 


 Request request = PostRequest.newInstance() .addPostParam("email", "some@gmail.com") .addPostParam("password", "any_password") .setLogTag("Login to server") .addGetParam(VAR_SIG, SIGNATURE) .setPath("auth2.php") .build(); 


 Request request = MultipartRequest.newInstance() .addTextBody("userName", "Alex") .addTextBody("email", "some@gmail.com") .addTextBody("password", "any_password") .addTextBody("sex", "male") .addJPEG("imagedata", bitmap, "image.jpg") .setLogTag("Create user") .addGetParam(VAR_SIG, SIGNATURE) .setPath("createuser.php") .build(); 


Processing query results and placing them in the object

The received request data can be processed by any of your favorite parsers. The processed data is placed in objects that implement the interfaces InputStreamDataInterface, StringDataInterface.

 public class LoginResult implements StringDataInterface { public static String token = ""; public static String email = ""; public static String password = ""; @Override public void fillFromString(String src) throws Exception { JSONObject jsonObject = new JSONObject(src); token = jsonObject.getString("token"); email = jsonObject.getString("email"); password = jsonObject.getString("password"); } 


Query execution and results

Request execution can be synchronous or asynchronous. A synchronous request returns a directly populated object created in the case of a successful request. Asynchronous request returns the same object or Exception in case of problems via Callback.

 DataProcessor.getInstance().executeAsync(request, LoginResult.class, handler); 


Callback processing example:

 private DataProcessor.Callback callback = new DataProcessor.Callback() { @Override public void onFinish(Object obj, int what) { if (what == HttpStatus.SC_OK) { ...   } else { Exception ex = (Exception) obj; if (ex instanceof IOException) { Log.e("IO Error", ex); } else { Log.e("Error", ex); } } } }; 


An example of the application can be viewed at link

Source: https://habr.com/ru/post/227723/


All Articles