欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android引用开源框架通过AsyncHttpClient实现文件上传

程序员文章站 2024-02-22 13:33:40
引用开源框架通过asynchttpclient进行文件上传,具体内容如下 一、步骤: 1.添加权限(访问网络权限和读写权限) 2.获取上传文件路径并判断是否为空...

引用开源框架通过asynchttpclient进行文件上传,具体内容如下

一、步骤:

1.添加权限(访问网络权限和读写权限)
2.获取上传文件路径并判断是否为空
3.若不为空,创建异步请求对象
4.创建上传文件路径
5.执行post请求(指定url路径,封装上传参数,新建asynchttpresponsehandler方法) 

二、查看参考文档 

Android引用开源框架通过AsyncHttpClient实现文件上传

三、实例项目解析

运行效果如下:

Android引用开源框架通过AsyncHttpClient实现文件上传

在本地文件夹中查看是否获取到图片,如下图显示

Android引用开源框架通过AsyncHttpClient实现文件上传

重点代码:均有详细解析,请认真查看注释
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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。