下面是我将文件上传到服务器的代码。但即使经过几次尝试,甚至在添加严格模式后,我也会遇到网络异常。
我是 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();
}
}