Android简易Http框架之ZzHttp

自己写的http框架。欢迎star

Github地址:www.github.com/zhouzhuo810/ZzHttp

ZzHttp

A http framework for simply GET and POST.

Gradle

compile 'me.zhouzhuo.zzhttp:zz-http:1.0.4'

Maven


  me.zhouzhuo.zzhttp
  zz-http
  1.0.4
  pom

How to use it ?

Don't forget adding perimssions.



If you want to see log info in logcat.

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        ZzHttp.setDebug(true);

    }
}

If there if only one baseUrl.

ZzHttp.getInstance().setBaseUrl("http://api.k780.com:88/")

If there is two or more baseUrl.

public class Api {

    private static final String ENDPOINT_ONE = "https://www.baidu.com/";
    private static final String ENDPOINT_TWO = "http://api.k780.com:88/";

    private static ZzHttp apiOne;
    private static ZzHttp apiTwo;

    public static ZzHttp getBaiduApi() {
        if (apiOne == null) {
            synchronized (Api.class) {
                if (apiOne == null) {
                    apiOne = ZzHttp.newInstance().setBaseUrl(ENDPOINT_ONE);
                }
            }
        }
        return apiOne;
    }

    public static ZzHttp getWeatherApi() {
        if (apiTwo == null) {
            synchronized (Api.class) {
                if (apiTwo == null) {
                    apiTwo = ZzHttp.newInstance().setBaseUrl(ENDPOINT_TWO);
                }
            }
        }
        return apiTwo;
    }
}

GET

return String

Api.getWeatherApi()
                .get(new HttpParams()
                        .addStringParam("app", "weather.today")
                        .addStringParam("weaid", "1")
                        .addStringParam("appkey", "10003")
                        .addStringParam("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4")
                        .addStringParam("format", "json"), String.class, new Callback.ZzCallback() {
                    @Override
                    public void onSuccess(String result) {
                        refreshLayout.setRefreshing(false);
                        tv.setText(result);
                    }

                    @Override
                    public void onFailure(String error) {
                        refreshLayout.setRefreshing(false);
                        Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();
                    }
                });

return Object

        Api.getWeatherApi()
                .get(new HttpParams()
                        .addStringParam("app", "weather.today")
                        .addStringParam("weaid", "1")
                        .addStringParam("appkey", "10003")
                        .addStringParam("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4")
                        .addStringParam("format", "json"), WeatherEntity.class, new Callback.ZzCallback() {
                    @Override
                    public void onSuccess(WeatherEntity result) {
                        refreshLayout.setRefreshing(false);
                        tv.setText(result.toString());
                    }

                    @Override
                    public void onFailure(String error) {
                        refreshLayout.setRefreshing(false);
                        Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();
                    }
                });

POST

return String

Api.getWeatherApi()
                .post(new HttpParams()
                        .addStringParam("app", "weather.today")
                        .addStringParam("weaid", "1")
                        .addStringParam("appkey", "10003")
                        .addStringParam("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4")
                        .addStringParam("format", "json"), String.class, new Callback.ZzCallback() {
                    @Override
                    public void onSuccess(String result) {
                        refreshLayout.setRefreshing(false);
                        tv.setText(result);
                    }

                    @Override
                    public void onFailure(String error) {
                        refreshLayout.setRefreshing(false);
                    }
                });

return Object

        Api.getWeatherApi()
                .post(new HttpParams()
                        .addStringParam("app", "weather.today")
                        .addStringParam("weaid", "1")
                        .addStringParam("appkey", "10003")
                        .addStringParam("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4")
                        .addStringParam("format", "json"), WeatherEntity.class, new Callback.ZzCallback() {
                    @Override
                    public void onSuccess(WeatherEntity result) {
                        refreshLayout.setRefreshing(false);
                        tv.setText(result.toString());
                    }

                    @Override
                    public void onFailure(String error) {
                        refreshLayout.setRefreshing(false);
                    }
                });

Download

        final String fileName = new File(filePath1).getName();
        String url = "http://192.168.1.102/test/image/" + fileName;
        final String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "test";
        ZzHttp.getInstance().download(url, dir, new Callback.ProgressDownloadCallback() {
            @Override
            public void onProgress(float progress, int currentSize, int totalSize) {
                tv_download_result.setText(progress + ", " + currentSize + "," + totalSize);
            }

            @Override
            public void onSuccess(String result) {
                ivDown.setImageURI(Uri.fromFile(new File(dir + File.separator + fileName)));
            }

            @Override
            public void onFailure(String error) {

            }
        });

Upload(multipart/form-data)

        ZzHttp.getInstance()
                .setBaseUrl("http://192.168.1.102/")
                .post("test/test.php", new HttpParams()
                        .setConnectTimeout(10000)
                        .setReadTimeout(10000)
                        .addBodyParam("file1", file1)
                        , UploadEntity.class, new Callback.ProgressUploadCallback() {

                    @Override
                    public void onProgress(float progress, int currentSize, int totalSize) {
                        tv_result.setText(progress + ", " + currentSize + "," + totalSize);
                    }

                    @Override
                    public void onSuccess(UploadEntity result) {
                        tv_result.setText(result.getCode() + "," + result.getData().getMsg());
                    }

                    @Override
                    public void onFailure(String error) {
                        Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();
                    }
                });

玩安卓版王者荣耀的朋友,刷金币看这里
链接:http://pan.baidu.com/s/1gfKIeh9
辅助群:171977523

需要写安卓程序的小伙伴请关注我的淘宝店铺:
小周来帮您

你可能感兴趣的:(Android简易Http框架之ZzHttp)