zoukankan      html  css  js  c++  java
  • Android采取async框架文件上传

    页面效果

    须要的权限

      <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);
    				}
    
    			});
    		}
    
    	}
    
    }
    

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


    框架代码下载

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




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

  • 相关阅读:
    .NET 分布式架构开发实战之一
    frame中隐藏横向滚动条
    实时检测网络状态及是否可以连接Internet
    jquery表格插件推荐
    FireFox窗体frameset,iframe间的js调用方法
    用C#实现实现简单的 Ping 的功能,用于测试网络是否已经联通
    一个阴历阳历互相转化的类
    CSS技巧 — 不使用图片实现圆角、阴影、渐变等功能
    Windows下命令行下启动ORACLE服务
    使用C#进行点对点通讯和文件传输(通讯基类部分)
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4891234.html
Copyright © 2011-2022 走看看