页面效果

须要的权限

<uses-permission android:name="android.permission.INTERNET"/>



网络訪问权限;

布局文件:

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" > <EditText
android:id="@+id/et_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startUplode"
android:text="@string/hello_world" /> </LinearLayout>

核心代码

package com.examp.uplodefile;

import java.io.File;
import java.io.FileNotFoundException; import org.apache.http.Header; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText; 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_url; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et_url = (EditText) findViewById(R.id.et_url);
} public void startUplode(View view) {
// 获取上传文件的地址
String filePath = et_url.getText().toString().trim();
// 创建出文件对象
File file = new File(filePath);
// 定义上传的server地址
String path = "http://172.22.64.193:8080/0001AndroidWebService/UplodeFileServlet";
// 推断文件的大小,及是否存在
if (file.exists() && file.length() > 0) {
// 使用开源框架,
//
AsyncHttpClient client = new AsyncHttpClient();
// 上传的參数
RequestParams params = new RequestParams();
try {
// 加入上传的数据
params.put("file", file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Upload a File
// 发送上传的消息,..并处理结果
client.post(path, params, new AsyncHttpResponseHandler() { @Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
System.out.println("==========" + statusCode);
for (int i = 0; i < headers.length; i++) {
System.out.println("&&&&&&" + headers[i]);
}
System.out.println("********" + new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
System.out.println("==========" + statusCode);
for (int i = 0; i < headers.length; i++) {
System.out.println("&&&&&&" + headers[i]);
}
System.out.println("********" + new String(responseBody));
System.out.println("--------" + error);
} });
} } }

编写前提是将框架代码已经导入到项目中

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTkzNjE0Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

框架代码下载

http://download.csdn.net/detail/u011936142/7424999

版权声明:本文博主原创文章,博客,未经同意不得转载。

Android采取async框架文件上传的更多相关文章

  1. ssh框架文件上传下载

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 第二百七十三节,Tornado框架-文件上传

    Tornado框架-文件上传 第一.普通表单上传文件 self.request.files["fafafa"] 获取上传文件信息,参数["上传文件框的name名称&quo ...

  3. SSH,SSM框架文件上传

    一.了解文件上传 1.1        什么是文件上传 将本地文件通过流的形式写到服务器上 1.2        文件上传的技术 JspSmartUpload: 其组件是应用jsp进行B/S程序开发过 ...

  4. Android开发之httpclient文件上传实现

    文件上传可能是一个比較耗时的操作,假设为上传操作带上进度提示则能够更好的提高用户体验,最后效果例如以下图: 项目源代码:http://download.csdn.net/detail/shinay/4 ...

  5. Android Retrofit 2.0文件上传

    Android Retrofit 实现(图文上传)文字(参数)和多张图片一起上传 使用Retrofit进行文件上传,肯定离不开Part & PartMap. public interface ...

  6. tp框架-----文件上传

    之前也做过文件上传,现在学了tp,用tp怎么做呢? 第一步:做一个Wenjian控制器: <?php namespace Ceshi\Controller; use Think\Controll ...

  7. 文件上传—SSM框架文件上传

    1.准备上传下载的api组件 <dependency> <groupId>commons-io</groupId> <artifactId>common ...

  8. tp 框架 -文件上传

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <?ph ...

  9. 文件上传—SSH框架文件上传

    1.准备上传的api组件 <dependency> <groupId>commons-io</groupId> <artifactId>commons- ...

随机推荐

  1. Python多线程2:sched

    sched模块提供了一个类的事件安排. scheduler类定义 class sched.scheduler(timefunc=time.monotonic, delayfunc=time.sleep ...

  2. SE 2014年4月8日

    1.路由引入的作用? 当网络中运行多种路由协议的时候,由于不同协议的路由算法和度量值等均不相同,路由引入可以将不同协议的路由引入到当前的路由协议中,保证网络的互通. 对比单向入和双向入 单向引入是只将 ...

  3. hdu1828(线段树——矩形周长并)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 分析:与面积不同的地方是还要记录竖的边有几个(num记录),并且当边界重合的时候需要合并(用lb ...

  4. follow through

    follow through是什么意思_follow through的翻译_音标_读音_用法_例句 - 必应 Bing Dictionary Web Images Videos Maps News D ...

  5. 阿尔贝我给我加i觉

    http://www.huihui.cn/share/8192186       http://www.huihui.cn/share/8192178       http://www.huihui. ...

  6. Netbeans源代码编辑技巧——使用代码补全和代码生成

    原文 Netbeans源代码编辑技巧——使用代码补全和代码生成 使用代码补全生成代码 一般来说,代码补全对于自动填充缺失的代码是有帮助的,例如标识符和关键字.截至 NetBeans IDE 6.0,您 ...

  7. vmware无法链接U盘:vm-->removeable devices.

    vmware无法链接U盘:vm-->removeable devices.

  8. maven项目建立pom.xml报无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3

    一.发现问题 建立maven项目后,pom.xml在显示红叉.鼠标放上去,显示Executiondefault-testResources of goalorg.apache.maven.plugin ...

  9. 本科非cs菜鸟计算机面试实录

    两年制小硕,本硕期间差不多都打酱油的.本科非cs专业,硕士cs,编程基础一般,专业基础尚可.研究生期间分析分析了pgsql数据库的源码:同时实验室一些杂项目:自己业余为了应试读了些计算机书.自己当时q ...

  10. Uva11464 开关问题

    给一个n×n的01矩阵,你的任务是将尽量少的0变成1,是的每个元素的上下左右的位置(如果存在的话)的之和均为偶数.1<=n<=15. 如果暴力整个矩阵,那么时间复杂度是O(2^(n*n)) ...