Code Monkey home page Code Monkey logo

husky-android-network-wrapper's Introduction

Husky-Android-Network-Wrapper

Provide basic http-methods(get,post,header),multi-thread uploading and downloading in callback mechanism.

#How to Run Demo 1.Run test server: python /TestServer/main.py
2.If server's internal network IP has changed in your computer,modify BaseUrl string in /example/constant/Web.java and rebuild
3.Run test Android class: /example/activity/MainActivity.java

#Usage 1.Firstly create the RequestQueue which sends http request in FIFO order.
   The RequestQueue is in synchronized block when it invokes push() and pop() method to ensure thread safety.

RequestQueue requestQueue=new RequestQueue();

2.GET method: override onSuccess() and onFailure() methods in Interface BaseCallback.
   Remember to add request to the requestQueue.

 String url= Web.BaseUrl+"/get_normal";
    HttpRequest request=new HttpRequest(url,new BaseCallback() {
        @Override
        public void onSuccess(Object response) {
            Log.i(TAG,"response: "+response);
        }

        @Override
        public void onFailure(Exception exception, int errCode) {
            Log.i(TAG,"exception: "+exception.toString());
            Log.i(TAG,"errCode: "+errCode);
        }
    });
    requestQueue.add(request);

3.POST method: override onSuccess() and onFailure() methods in Interface BaseCallback.

 String url= Web.BaseUrl+"/post_normal";
    PostParams params=new PostParams();
    params.put("age", "2");
    params.put("name","Jack");
    params.put("weight", "98");
    HttpRequest request=new HttpRequest(url,params,new BaseCallback() {
        @Override
        public void onSuccess(Object response) {
            Log.i(TAG,"response: "+response);
        }

        @Override
        public void onFailure(Exception exception, int errCode) {
            Log.i(TAG,"exception: "+exception.toString());
            Log.i(TAG,"errCode: "+errCode);
        }
    });
    requestQueue.add(request);

4.HEAD method: override onSuccess() and onFailure() methods in Interface HeadCallback.

 HttpRequest request=new HttpRequest(url,new HeadCallback() {
        @Override
        public void onSuccess(Object response) {
            Log.i(TAG,((Content)response).toString());
        }

        @Override
        public void onFailure(Exception exception, int errCode) {
            Log.i(TAG,"exception: "+exception.toString());
            Log.i(TAG,"errCode: "+errCode);
        }
    });
    requestQueue.add(request);

5.File download: override onProgress(),onSuccess() and onFailure() methods in Interface FileDownloadCallBack.
   The onProgress() method is for acquring current doanload length in the process of downloading.This method can be used to draw a progress bar.

String url="http://www.baidu.com/img/bd_logo1.png";
final File file=new File(Environment.getExternalStorageDirectory().getPath()+"/baidu.jpg");
int threadNum=10;
FileDownloadParam param=new FileDownloadParam(file,threadNum);
HttpRequest request=new HttpRequest(url,param,new FileDownloadCallBack()
{
    @Override
    public void onProgress(int fileLength, int curLength) {
        Log.i(TAG,"progress: fileLength: "+fileLength+" curLength: "+curLength); 
    }

    @Override
    public void onSuccess(Object response) {
        Log.i(TAG,((File)response).getName()+" download success");
    }

    @Override
    public void onFailure(Exception exception, int errCode) {
        Log.i(TAG,"exception: "+exception.toString());
        Log.i(TAG,"errCode: "+errCode);
    }
});
requestQueue.add(request);

6.File upload: override onProgress(),onSuccess() and onFailure() methods in Interface FileUploadCallback.
   Files upload in the form of map< String,File>.The key string (like "test","baidu") is used for extracting specific file from post-form in server. So it isn't merely confined to file name.
   The onProgress() method is for acquring current file index and upoad length in the process of uploading.This method can be used to draw an upload progress bar.

String url= Web.BaseUrl+"/upload_file";
HashMap<String, File> map=new HashMap<String,File>();
map.put("test",new File(Environment.getExternalStorageDirectory().getPath()+"/test.jpg"));
map.put("baidu",new File(Environment.getExternalStorageDirectory().getPath()+"/baidu.jpg"));
FileUploadParams params=new FileUploadParams(map);
HttpRequest request=new HttpRequest(url,params,new FileUploadCallback() {
    @Override
    public void onProgress(int fileCount, int curFile, long count, long cur) {
        Log.i(TAG,"progress: totalFile:"+fileCount+" curFile:"+curFile+" fileLength:"+count+" curLength:"+cur);
    }

    @Override
    public void onSuccess(Object response) {
        Log.i(TAG,"upload success: "+response);
    }

    @Override
    public void onFailure(Exception exception, int errCode) {
        Log.i(TAG,"exception: "+exception.toString());
        Log.i(TAG, "errCode: " + errCode);
    }
});
requestQueue.add(request);    

husky-android-network-wrapper's People

Contributors

didihe1988 avatar

Stargazers

Allen_3 avatar Zhonghui You avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.