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

下面是我上传文件到服务器的代码。但即使经过多次尝试,甚至在添加严格模式之后,我仍然会收到网络异常。如何使用异步任务将文件上传到服务器?

我是新来的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();

}

}

2012-05-30

IMRAN

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值