package xunqaing.bwie.com.afinal_xutils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.io.File;
public class MainActivity extends Activity {
private Button bt_download;
private Button bt_upload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
bt_download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downLoad();
}
});
bt_upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
upload();
}
});
}
//上传
private void upload() {
//从哪上传
String path = Environment.getExternalStorageDirectory() + "/xunxun.jpg";
//上传到哪
String url = "http://qhb.2dyt.com/Bwei/upload";
//设置请求参数
RequestParams requestParams = new RequestParams(url);
String[] arr = path.split("/");
//上传的文件名称
requestParams.addBodyParameter("imageFileName", arr[arr.length - 1]);
//网址参数
requestParams.addBodyParameter("username", "111");
requestParams.addBodyParameter("pwd", "123456");
requestParams.addBodyParameter("age", "23");
//传文件
requestParams.addBodyParameter("image", new File(path));
x.http().post(requestParams, new Callback.CacheCallback<String>() {
@Override
public void onSuccess(String result) {
Log.d("msg",result);
System.out.println("========================================");
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
@Override
public boolean onCache(String result) {
return false;
}
});
// HttpHandler httpHandler = httpUtils.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack() {
// @Override
// public void onSuccess(ResponseInfo responseInfo) {
// Log.d("MSG","======onSuccess====="+responseInfo.result.toString());
// }
//
// @Override
// public void onFailure(HttpException e, String s) {
// Log.d("MSG","======onFailure====="+s);
// }
// });
}
private void downLoad() {
// 中间那俩参数貌似是支持断点下载的参数
// HttpHandler handler = httpUtils.download("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492407259121&di=46cf2a411935b18e1997650d29734b7d&imgtype=0&src=http://pic.92to.com/anv/201512/16/heu4ac4qoci.jpg",
//
// Environment.getExternalStorageDirectory()+"/cooka.jpg",
// true,
// true,
// new RequestCallBack<File>() {
// @Override
// public void onSuccess(ResponseInfo<File> responseInfo) {
// Log.d("MSG",responseInfo.result.getPath());
// }
//
// @Override
// public void onFailure(HttpException e, String s) {
// Log.d("MSG",s);
// }
// });
String url = "http://img02.tooopen.com/images/20160408/tooopen_sy_158723161481.jpg";
//参数设置
RequestParams requestParams = new RequestParams(url);
//设置保存文件位置
requestParams.setSaveFilePath(Environment.getExternalStorageDirectory()+"/2.jpg");
//发送请求
x.http().get(requestParams, new Callback.CommonCallback<File>() {
@Override
public void onSuccess(File file) {
System.out.println("========================================");
}
@Override
public void onError(Throwable throwable, boolean b) {
}
@Override
public void onCancelled(CancelledException e) {
}
@Override
public void onFinished() {
}
});
}
private void initView() {
bt_download = (Button) findViewById(R.id.bt_download);
bt_upload = (Button) findViewById(R.id.bt_upload);
}
}