ZzHttp,一个简洁的Http框架

自己写的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.0'

Maven

<dependency>
  <groupId>me.zhouzhuo.zzhttp</groupId>
  <artifactId>zz-http</artifactId>
  <version>1.0.0</version>
  <type>pom</type>
</dependency>


How to use it ?

Don’t forget adding perimssions.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />



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<String>() {
                    @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<WeatherEntity>() {
                    @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<String>() {
                    @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<WeatherEntity>() {
                    @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<UploadEntity>() {

                    @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();
                    }
                });
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq244570672/article/details/52384781
文章标签: Http Android
个人分类: Android-网络编程
想对作者说点什么? 我来说一句

http简易框架

2016年02月04日 979KB 下载

Okhttp框架

2017年04月12日 361KB 下载

一个简单的登录页面

2014年03月03日 4KB 下载

okhttp框架

2016年08月02日 2.19MB 下载

android-async-http-master

2015年01月10日 560KB 下载

没有更多推荐了,返回首页

不良信息举报

ZzHttp,一个简洁的Http框架

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭