Android采用async框架实现文件上传
来源:互联网 发布:java添加图片代码 编辑:程序博客网 时间:2024/05/22 02:07
页面效果
需要的权限
<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;@Overrideprotected 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);// 定义上传的服务器地址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 blocke.printStackTrace();} // Upload a File// 发送上传的消息,..并处理结果client.post(path, params, new AsyncHttpResponseHandler() {@Overridepublic 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));}@Overridepublic 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);}});}}}
编写前提是将框架代码已经导入到项目中
框架代码下载
http://download.csdn.net/detail/u011936142/7424999
2 0
- Android采用async框架实现文件上传
- Android-Async-Http(1)—上传文件
- android开源框架andorid-async-http使用(下载上传文件)
- android开源框架andorid-async-http使用(下载上传文件)
- 网络编程---Android开源组件(Async-http)实现文件上传
- 采用FTP协议实现文件的上传
- 采用HTTP协议上传文件实现(java)
- Android上传文件 服务器端采用php
- Android:网络:采用FTP上传文件
- android-async-http使用一个参数上传多个文件
- android-async-http byte字节数组上传文件
- Android笔记 采用async http 框架 提交数据到服务器demo
- android-async-http框架
- android-async-http框架
- SSH:Struts1框架(采用struts上传文件)
- Android使用开源框架实现文件的上传 asyncHttpClient
- 利用HTTP协议实现Android文件上传至WEB服务器,采用PHP接收文件(参考网上自己实现)
- 采用struts上传文件
- 详解Android Activity 生命周期
- chromium代码git过程错误记录
- IO复用\阻塞IO\非阻塞IO\同步IO\异步IO
- 【Leetcode】Generate Parentheses
- Qt与文件读取操作相关。
- Android采用async框架实现文件上传
- c# Winform 用到的一个打印控件(VBprinter.DGVprint)。
- [linux][tool] 容易忘记的cmd命令
- winform直接显示二进制数据中的图片
- android圆角图片和图片倒影
- 关于我
- Qt常用快捷键
- Oracle 日期处理
- C++ Primer Plus 第六版 第10章 对象和类