Android引用开源框架通过AsyncHttpClient实现文件上传
作者:lijiao 时间:2021-09-22 20:02:05
引用开源框架通过AsyncHttpClient进行文件上传,具体内容如下
一、步骤:
1.添加权限(访问网络权限和读写权限)
2.获取上传文件路径并判断是否为空
3.若不为空,创建异步请求对象
4.创建上传文件路径
5.执行post请求(指定url路径,封装上传参数,新建AsyncHttpResponseHandler方法)
二、查看参考文档
三、实例项目解析
运行效果如下:
在本地文件夹中查看是否获取到图片,如下图显示
重点代码:均有详细解析,请认真查看注释
1、在AndroidManifest.xml中添加权限
<uses-permission Android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
2、布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件上传" />
<EditText
android:id="@+id/et_upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:ems="10"
android:text="/storage/sdcard0/1.jpg">
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_upload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_upload"
android:onClick="upload"
android:text="上传文件" />
</RelativeLayout>
3、MainActivity.java
package com.example.android_upload;
import java.io.File;
import org.apache.http.Header;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class MainActivity extends Activity {
private EditText et_file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
et_file = (EditText) findViewById(R.id.et_upload);
}
//点击上传按钮
public void upload(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_upload:
//获取上传文件的路径
String path = et_file.getText().toString();
//判断上次路径是否为空
if (TextUtils.isEmpty(path.trim())) {
Toast.makeText(this, "上次文件路径不能为空", 1).show();
} else {
//异步的客户端对象
AsyncHttpClient client = new AsyncHttpClient();
//指定url路径
String url = "http://172.16.237.144:8080/Login/UploadServlet";
//封装文件上传的参数
RequestParams params = new RequestParams();
//根据路径创建文件
File file = new File(path);
try {
//放入文件
params.put("profile_picture", file);
} catch (Exception e) {
// TODO: handle exception
System.out.println("文件不存在----------");
}
//执行post请求
client.post(url,params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
if (statusCode == 200) {
Toast.makeText(getApplicationContext(), "上次成功", 1)
.show();
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
error.printStackTrace();
}
});
}
break;
default:
break;
}
}
}
重点代码就是这些,自己动手查看一下效果吧!~
开源框架资源:http://xiazai.jb51.net/201701/yuanma/AndroidAsyncHttpClient(jb51.net).rar
源码:http://xiazai.jb51.net/201701/yuanma/AsyncHttpClient(jb51.net).rar
来源:http://blog.csdn.net/jdsjlzx/article/details/41694323
标签:Android,AsyncHttpClient,文件上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
分析HashMap 的 JDK 源码
2022-01-17 06:29:54
![](https://img.aspxhome.com/file/2023/4/68164_0s.jpg)
Java ThreadLocal类应用实战案例分析
2022-07-11 19:04:36
在Ubuntu中安装VSCode并配置C/C++开发环境的方法步骤
2021-07-04 18:19:19
spring boot使用thymeleaf跳转页面实例代码
2021-09-14 09:46:16
![](https://img.aspxhome.com/file/2023/1/122261_0s.png)
IDEA代码规范插件P3C+代码注释模板配置方法
2022-10-30 17:32:55
![](https://img.aspxhome.com/file/2023/8/86838_0s.png)
c#根据文件大小显示文件复制进度条实例
2022-10-10 10:35:16
![](https://img.aspxhome.com/file/2023/5/116575_0s.png)
每日六道java新手入门面试题,通往自由的道路
2023-11-13 16:22:29
Java中用爬虫进行解析的实例方法
2021-06-23 09:07:00
Java JUC中操作List安全类的集合案例
2022-10-28 11:09:31
![](https://img.aspxhome.com/file/2023/7/83437_0s.png)
Java Swing JTextArea文本区域的实现示例
2023-10-30 13:40:28
![](https://img.aspxhome.com/file/2023/6/58606_0s.png)
MyBatis 如何获取子类的属性
2022-09-12 14:42:39
![](https://img.aspxhome.com/file/2023/0/63650_0s.png)
SpringBoot统一返回JSON格式实现方法详解
2021-10-03 20:22:52
Intellij IDEA 2017新特性之Spring Boot相关特征介绍
2023-06-22 15:13:02
![](https://img.aspxhome.com/file/2023/0/88140_0s.gif)
GraphQL入门总体创建教程
2022-10-16 12:29:09
![](https://img.aspxhome.com/file/2023/3/117683_0s.png)
Java微信公众平台开发(12) 微信用户信息的获取
2023-05-26 07:28:56
![](https://img.aspxhome.com/file/2023/4/93144_0s.jpg)
Java常用工具类汇总 附示例代码
2022-07-21 17:54:15
![](https://img.aspxhome.com/file/2023/1/118891_0s.png)
java生成jar包并且单进程运行的实例
2021-08-30 00:40:55
C# WPF Image控件的绑定方法
2023-09-03 06:41:42
Android自定义ViewGroup实现弹性滑动效果
2022-07-25 19:44:08
![](https://img.aspxhome.com/file/2023/8/122078_0s.jpg)
SSM使用mybatis分页插件pagehepler实现分页示例
2022-04-02 00:20:53