自己写的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
<dependency>
<groupId>me.zhouzhuo.zzhttp</groupId>
<artifactId>zz-http</artifactId>
<version>1.0.4</version>
<type>pom</type>
</dependency>
<h1>How to use it ?</h1>
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;
}
}
<h3>GET</h3>
① 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();
}
});
<h3>POST</h3>
①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);
}
});
<h3>Download</h3>
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) {
}
});
<h3>Upload(multipart/form-data)</h3>
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();
}
});
玩安卓版王者荣耀的朋友,刷金币看这里
链接:http://pan.baidu.com/s/1gfKIeh9
辅助群:171977523
需要写安卓程序的小伙伴请关注我的淘宝店铺:
小周来帮您
网友评论