依赖库地址:https://github.com/hongyangAndroid/okhttputils
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="lianyuan.com.okhttpdemo.MainActivity">
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="okhttp-utlis:get" />
<Button
android:id="@+id/btn_post"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="okhttp-utlis:get" />
<Button
android:id="@+id/btn_downloadFile"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="okhttp-utlis:downloadFile" />
<Button
android:id="@+id/btn_multiFileUpload"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="okhttp-utlis:multiFileUpload" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"/>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
package lianyuan.com.okhttpdemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Request;
import static android.content.ContentValues.TAG;
public class MainActivity extends Activity implements View.OnClickListener {
private TextView tv;
private Button btn_get;
private Button btn_post;
private String mBaseUrl = "http://192.168.31.242:8888/okHttpServer/";
private Button btn_downloadFile;
private ProgressBar progressBar;
private Button btn_multiFileUpload;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
btn_get = (Button) findViewById(R.id.btn_get);
btn_post = (Button) findViewById(R.id.btn_post);
btn_downloadFile = (Button) findViewById(R.id.btn_downloadFile);
btn_multiFileUpload = (Button) findViewById(R.id.btn_multiFileUpload);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btn_get.setOnClickListener(this);
btn_post.setOnClickListener(this);
btn_downloadFile.setOnClickListener(this);
btn_multiFileUpload.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_get://get请求
getOkhttpUtlis();
break;
case R.id.btn_post://post请求
postOkhttpUtlis();
break;
case R.id.btn_downloadFile://下载大文件
downLoadFile();
break;
case R.id.btn_multiFileUpload://上传多文件
multiFileUpload();
break;
}
}
public void getOkhttpUtlis() {
String url = "http://www.zhiyun-tech.com/App/Rider-M/changelog-zh.txt";
url = "http://www.391k.com/api/xapi.ashx/info.json?key=bd_hyrzjjfb4modhj&size=10&page=1";
OkHttpUtils
.get()
.url(url)
.id(100)
.build()
.execute(new MyStringCallback());
}
public void postOkhttpUtlis() {
String url = "http://www.zhiyun-tech.com/App/Rider-M/changelog-zh.txt";
url = "http://www.391k.com/api/xapi.ashx/info.json?key=bd_hyrzjjfb4modhj&size=10&page=1";
OkHttpUtils
.post()
.url(url)
.id(100)
.build()
.execute(new MyStringCallback());
}
private void downLoadFile() {
String url = "http://vfx.mtime.cn/Video/2016/07/24/mp4/160724055620533327_480.mp4";
OkHttpUtils//
.get()//
.url(url)//
.build()//
.execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "okhttp-utils-test.mp4")//
{
@Override
public void onBefore(Request request, int id) {
}
@Override
public void inProgress(float progress, long total, int id) {
progressBar.setProgress((int) (100 * progress));
Log.e(TAG, "inProgress :" + (int) (100 * progress));
}
@Override
public void onError(Call call, Exception e, int id) {
Log.e(TAG, "onError :" + e.getMessage());
}
@Override
public void onResponse(File file, int id) {
Log.e(TAG, "onResponse :" + file.getAbsolutePath());
}
});
}
public void multiFileUpload() {
File file = new File(Environment.getExternalStorageDirectory(), "messenger_01.png");
File file2 = new File(Environment.getExternalStorageDirectory(), "test1#.txt");
if (!file.exists()) {
Toast.makeText(MainActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show();
return;
}
Map<String, String> params = new HashMap<>();
params.put("username", "张鸿洋");
params.put("password", "123");
String url = mBaseUrl + "user!uploadFile";
OkHttpUtils.post()//
.addFile("mFile", "messenger_01.png", file)//
.addFile("mFile", "test1.txt", file2)//
.url(url)
.params(params)//
.build()//
.execute(new MyStringCallback());
}
public class MyStringCallback extends StringCallback {
@Override
public void onBefore(Request request, int id) {
setTitle("loading...");
}
@Override
public void onAfter(int id) {
setTitle("Sample-okHttp");
}
@Override
public void onError(Call call, Exception e, int id) {
e.printStackTrace();
tv.setText("onError:" + e.getMessage());
}
@Override
public void onResponse(String response, int id) {
Log.e(TAG, "onResponse:complete" + response);
tv.setText("onResponse:" + response);
switch (id) {
case 100:
Toast.makeText(MainActivity.this, "http", Toast.LENGTH_SHORT).show();
break;
case 101:
Toast.makeText(MainActivity.this, "https", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void inProgress(float progress, long total, int id) {
Log.e(TAG, "inProgress:" + progress);
// mProgressBar.setProgress((int) (100 * progress));
}
}
}