您的位置:首页 > 理论基础 > 计算机网络

找呀志_通过开源框架引AsyncHttpClient上传文件

2015-07-14 10:48 806 查看

一个、步骤:

1.加入权限(接入网络和可写)

2.获取上传文件的路径和推断是空的

3.如果为空。创建一个异步请求对象

4.创建上传文件路径

5.跑post请求(指定url路径。封装上传參数。新建AsyncHttpResponseHandler方法)

二、查看參考文档



三、实例项目解析

执行效果例如以下:





在本地目录中查看是否获取到图片,例如以下图显示



重点代码:均有具体解析。请认真查看凝视

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_height="wrap_content"
android:layout_below="@+id/textView1"
android:ems="10"
android:text="/storage/sdcard0/1.jpg">

<requestFocus />
</EditText>

<Button
android:id="@+id/btn_upload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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);
}

//点击上传button
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();
}
}

@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
error.printStackTrace();
}
});

}
break;

default:
break;
}

}

}


重点代码就是这些。自己动手查看一下效果吧!~

开源框架资源:http://download.csdn.net/detail/zhaoyazhi2129/7400787

源代码:http://download.csdn.net/detail/zhaoyazhi2129/7400811

转发请标明原文地址/article/1344678.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: