您的位置:首页 > 理论基础 > 计算机网络

AsyncHttpClient上传文件到服务器 注意6.0权限

2017-06-15 17:27 627 查看
实际测试,上传大文件的话,总是链接超时,,,

还是换okhttp3…

github

https://github.com/AsyncHttpClient/async-http-client/

这个AsynchHttpClient的好处很多,特别是写起来特别简洁,不用让你访问网络开个子线程,更改界面的信息再搞个handler。

代码:

public void postFile() throws Exception{
String path =""; // 文件路径
String url = "http://192.168.1.100:8080/web/UploadFile"; // 上传地址
File file = new File(path);
if(file.exists() && file.length()>0){
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("file", file); // 参数要对应
client.post(url, params,new AsyncHttpResponseHandler() {

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_LONG).show();
}

@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_LONG).show();
}
});
}else{
Toast.makeText(this, "文件不存在", Toast.LENGTH_LONG).show();
}

}


示例代码

android端

https://github.com/dzetAndroid/LearnUploadFile

web javaee端

http://download.csdn.net/detail/qqduxingzhe/9871700

来源

http://www.tuicool.com/articles/AnaaMbA

javaee struts2参考文章

http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990674.html



特别注意6.0的权限,

Android 6.0 运行时权限管理最佳实践

http://blog.csdn.net/yanzhenjie1003/article/details/52503533

github

AndPermission

6.0权限第三方lib

https://github.com/yanzhenjie/AndPermission
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: