Monday 26 December 2016

Android Networking Library- Android Networking Library


One Line Library is a power android networking library. This
library can be imported in any android existing project using
following
With an aim to minimize developer hardwork this library will help
them to focus on their UI rather then handling network error
themselves.
At present library supports the following:
1. VolleyWithOKHTTP Request
2. ImageUpload
3. Download Image
4. Post Request
5. Get Request
6. Cancel / Abort Request
7. JSON Object Request
8. Network Health Status

 Download and use the library now:

 Maven:
        <dependency>
        <groupId>onelinelibrary.com.onelinelibrary</groupId>
        <artifactId>onelinelibrary</artifactId>
        <version>1.0.0</version>
        <type>pom</type>
        </dependency>

 Gradle:

compile 'onelinelibrary.com.onelinelibrary:onelinelibrary:1.0.0'


Tutorial :

  • VolleywithOKHTTP:
As we know volley by Google is well known for its network cancel feature and OKHTTP for its speed and performance. We tried to integrate both into one function by modifying the Volley function to use OKHTTP Response.
StringRequest stringRequest = new StringRequest("http://resolvefinance.co.uk/Whatsapp/json3.txt",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("Response returned is","-"+response);
try{
parseJSON(response);
}
catch (Exception e){
Toast.makeText(c1,"Please check your Internet
Connection",Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show(); Log.i("Volley ka error", "" + error); } }); RequestQueue requestQueue = Volley.newRequestQueue(c1, new OkHttpStack(new com.squareup.okhttp.OkHttpClient())); requestQueue.add(stringRequest);


  • Getting response from API() 
Getting reponse from API is now very simple. Just add the URL of the API to the load parameter and pass all the query parameters as key value pairs, the response could be parsed as String, JSON object ,JSON Array, Bitmap etc.


OneLineLibrary.load("http://jiittute.esy.es/getContents.php")
.setTag("LoadingAPI")
.addQueryParameter("userid","1")
.setPriority(Priority.LOW)
.start()
.getAsString(new StringRequestListener () {
@Override
public void onResponse(String response) {
Log.d(TAG, "onResponse API"+response);
}
@Override
public void onError(ANError error) {
Log.d(TAG, "ERROR aPI");
}
});

  • ImageUpload() 

Use this to upload image from your internal storage of your phone. For instance suppose you have an image name screens.png located at following path
―Environment.getExternalStorageDirectory().getPath()+"/MAJOR/screens.jpg"
We can push the following file directly to servers by writing the following one Line function.


 File file=new
File(Environment.getExternalStorageDirectory().getPath()+"/MAJOR/screens.jpg");
OneLineLibrary.upload("http://jiittute.esy.es/MAJOR/postFile.php")
.parameter("pic",file)
.setTag("uploadTest")
.start()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
// do anything with response
Log.d("MAJOR","IMAGE_REQUEST MADE"+response.toString());
}
@Override
public void onError(ANError error) {
// handle error
Log.d("MAJOR","IMAGE_REQUEST MADE"+error.getMessage());
}
});



  • LoadImageDirectly()-

Use this to download image as a Bitmap response from the network.



 OneLineLibrary.load("http://jiittute.esy.es/MAJOR/uploads/screens.jpg")
.setTag("imageRequestTag")
.setPriority(Priority.LOW)
.setBitmapConfig(Bitmap.Config.ARGB_8888)
.start()
.getAsBitmap(new BitmapRequestListener() {
@Override
public void onResponse(Bitmap response) {
Log.d(TAG, "onResponse Bitmap");
}
@Override
public void onError(ANError error) {
Log.d(TAG, "ERROR Bitmap");
}
});



  • Cancel/Abort Network Request

OneLineLibrary.cancelAll();




  • Network Health Status - Calling this function will return you the network heath

    • status. In background it downloads the 1MB file from the remote servers and calculate
        time. Based on this it calculate bandwidth of the network that ie. Bits transferred per
          second.
            Additional Features
              1. ―Priority TYPES : User can choose from the multiple request time which
                includes LOW, MEDIUM, HIGH and IMMEDIATE.
                  2. TAG name: You can assign Tag with your network call and can later cancel it
                    using.


                    3. ProgressListner: Some of the modules include progress listeners by

                    calculating received bytes and send bytes;



                    cancelRequestWithGivenTag(tagname);


                    GITHUB CODE:

                    Visit:  https://github.com/subhamG98/Android-Parse-JSON-in-one-line-function


                    License:

                    Licensed under the Apache License, Version 2.0 (the "License");
                    you may not use this file except in compliance with the License.
                    You may obtain a copy of the License at

                       http://www.apache.org/licenses/LICENSE-2.0

                    Unless required by applicable law or agreed to in writing, software
                    distributed under the License is distributed on an "AS IS" BASIS,
                    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
                    See the License for the specific language governing permissions and limitations under the License.



                    No comments:

                    Post a Comment