页面效果

须要的权限

<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. VC生成的DLL给QT的EXE调用时lib路径问题小结

    VC生成的DLL给QT调用,有两种方式,一种是隐式调用调用(使用.lib文件方式): ① 在*.pro工程文件中添加VC生成的lib文件路径时,或者使用一个绝对路径,如: LIBS += " ...

  2. gant

    http://gant.github.io/ http://gant.codehaus.org/

  3. hdu 2842 Chinese Rings

    点击打开hdu2842 思路: 矩阵快速幂 分析: 1 题目的意思是给定n个环,和一些规则要把所有的环全部拆下最少需要的步数 2 题目规定如果要拆第n个环,那么第n-1个要挂着,n-2环要被拆下.那么 ...

  4. LINUX编程学习笔记(十四) 创建进程与 父子进程内存空间

    1什么是进程:进程是一个执行中的程序 执行的程序: 代码->资源->CPU 进程有很多数据维护:进程状态/进程属性 所有进程属性采用的一个树形结构体维护 ps  -a//所有进程 ps - ...

  5. 不起眼的 z-index 却能牵扯出这么大的学问(转)

    z-index在日常开发中算是一个比较常用的样式,一般理解就是设置标签在z轴先后顺序,z-index值大的显示在最前面,小的则会被遮挡,是的,z-index的实际作用就是这样. 但是你真的了解z-in ...

  6. Java读取本地文件,并显示在JSP文件中

        当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相对路径,还可以是网络上的图片路径.在存 ...

  7. centos 6.3 vnc连接—— catalog is not properly configured, attempting to determine an appropriate font p

    摘要:linux环境下,利用VNC连接远程桌面是经常用到的.这里,我们介绍centos上,利用VNC连接远程桌面的方法和常见的两个问题的解决方法1)由于字体问题,导致VNCserver无法启动 2)由 ...

  8. 关于identifier was truncated to '255' characters

    学习c++过程中,遇到在VC中使用set时DEBUG模式出现的警告 identifier was truncated to '255' characters in the debug informat ...

  9. SWT中一些细节的说明

    1.shell.pack();函数的效果是将程序打包,使其以合适的大小显示出来,不会太大有很多多余空间,也不会太小导致有些控件无法显示. 2. while(!shell.isDisposed()){ ...

  10. 解决backgroud:transparent在低版本浏览器中的bug

    今天在html页面上定义了一个button和一个div,大小相同,button使用绝对定位,覆盖在div上面一层,同时样式设置背景透明(background:transparent). 这样就可以在看 ...