如何使用上传到服务器的 文件,如何使用异步任务将文件上传到服务器?

该篇博客是一位Android新手在遇到网络上传文件异常时的求助,寻求帮助解决代码中可能存在的同步问题,并询问如何在上传文件操作中引入异步任务以提高性能。作者使用了Apache HttpClient进行HTTP请求,希望找到可能的错误并学习异步网络操作的最佳实践。
摘要由CSDN通过智能技术生成

以下是我用于将文件上传到服务器的代码。但是,即使经过几次尝试,甚至添加了严格模式,我都遇到了网络异常。

我是Android的新手,不知道如何使用异步任务,许多人建议您使用这种异步网络操作。谁能告诉我代码中哪里有错误以及应该在哪里使用异步任务?

package de.fileuploader;

import java.io.BufferedReader;

import java.io.File;

import java.io.InputStreamReader;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.mime.HttpMultipartMode;

import org.apache.http.entity.mime.MultipartEntity;

import org.apache.http.entity.mime.content.ByteArrayBody;

import org.apache.http.entity.mime.content.FileBody;

import org.apache.http.entity.mime.content.StringBody;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.protocol.BasicHttpContext;

import org.apache.http.protocol.HttpContext;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.os.StrictMode;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

@SuppressWarnings("unused")

public class Android_helloActivity extends Activity {

private String newName = "SMSBackup.txt";

private String uploadFile = "/mnt/sdcard/SMSBackup.txt";

private String actionUrl = "http://192.168.1.8:8080/admin/admin/uploads";

// private String

// actionUrl="http://upload-file.shcloudapi.appspot.com/upload";

private TextView mText1;

private TextView mText2;

private Button mButton;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()

.detectDiskReads()

.detectDiskWrites()

.detectNetwork() // or .detectAll() for all detectable problems

.penaltyLog()

.build());

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()

.detectLeakedSqlLiteObjects()

.detectLeakedClosableObjects()

.penaltyLog()

.penaltyDeath()

.build());

mText1 = (TextView) findViewById(R.id.myText2);

mText1.setText("Upload\n" + uploadFile);

mText2 = (TextView) findViewById(R.id.myText3);

mText2.setText("To Server Location\n" + actionUrl);

mButton = (Button) findViewById(R.id.myButton);

mButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

/* uploadFile(); */

try {

HttpClient httpClient = new DefaultHttpClient();

HttpContext localContext = new BasicHttpContext();

HttpPost httpPost = new HttpPost(actionUrl);

MultipartEntity entity = new MultipartEntity(

HttpMultipartMode.BROWSER_COMPATIBLE);

entity.addPart("name", new StringBody(newName));

File file=new File(uploadFile);

entity.addPart("file", new FileBody(file));

//entity.addPart("file", new

ByteArrayBody(data,"myImage.jpg"));

entity.addPart("gps", new StringBody("35.6,108.6"));

httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost,

localContext);

BufferedReader reader = new BufferedReader(

new

InputStreamReader(response.getEntity().getContent(),

"UTF-8"));

String sResponse = reader.readLine();

Log.i("info", "test");

} catch (Exception e) {

// Log.e("exception", e.printStackTrace());

e.printStackTrace();

showDialog("" + e);

}

}

});

}

private void showDialog(String mess) {

new AlertDialog.Builder(Android_helloActivity.this).setTitle("Message")

.setMessage(mess)

.setNegativeButton("Exit", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

}

}).show();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值