Welcome

首页 / 移动开发 / Android / Android引用开源框架通过AsyncHttpClient实现文件上传

引用开源框架通过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; @Overrideprotected 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 exceptionSystem.out.println("文件不存在----------"); } //执行post请求 client.post(url,params, new AsyncHttpResponseHandler() { @Overridepublic void onSuccess(int statusCode, Header[] headers,byte[] responseBody) { if (statusCode == 200) {Toast.makeText(getApplicationContext(), "上次成功", 1).show(); }} @Overridepublic 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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。