Android Data Processor Library for easy building and processing REST requests

    In everyday work, I constantly face the development of applications using REST services. Existing libraries that help in building queries and processing them did not suit me very well for a number of reasons. There was an idea to create a simple tool like Universal Image Loader that allows you to quickly build queries and parse received 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 performed 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 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 request parameters according to http://developer.android.com/reference/java/net/URL.html , encoding, timeout, etc. This data is basic and can be easily modified when building specific queries. Initialization is most conveniently performed in a 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 and also process local files.

    Request creation

    
    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 the query results and placing them in the object

    The received query 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");
    	}
    


    Fulfilling a query and obtaining results

    Fulfilling a query can be synchronous or asynchronous. A synchronous request returns a directly populated object created in case of a successful request. An 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 application can be viewed at the link

    Also popular now: