天天看點

引用開源架構通過AsyncHttpClient進行檔案上傳

1.添權重限(通路網絡權限和讀寫權限)

2.擷取上傳檔案路徑并判斷是否為空

3.若不為空,建立異步請求對象

4.建立上傳檔案路徑

5.執行post請求(指定url路徑,封裝上傳參數,建立asynchttpresponsehandler方法)

引用開源架構通過AsyncHttpClient進行檔案上傳

運作效果如下:

引用開源架構通過AsyncHttpClient進行檔案上傳
引用開源架構通過AsyncHttpClient進行檔案上傳

在本地檔案夾中檢視是否擷取到圖檔,如下圖顯示

引用開源架構通過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_below="@+id/textview1"  

        android:ems="10"   

        android:text="/storage/sdcard0/1.jpg">  

        <requestfocus />  

    </edittext>  

    <button  

        android:id="@+id/btn_upload"  

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

                        }  

                    }  

                    public void onfailure(int statuscode, header[] headers,  

                            byte[] responsebody, throwable error) {  

                        error.printstacktrace();  

                });  

            }  

            break;  

        default:  

        }  

}  

重點代碼就是這些,自己動手檢視一下效果吧!~

開源架構資源:http://download.csdn.net/detail/zhaoyazhi2129/7400787

源碼:http://download.csdn.net/detail/zhaoyazhi2129/7400811

轉發請标明原文位址http://blog.csdn.net/zhaoyazhi2129/article/details/27048149

繼續閱讀