Dưới đây là mã của tôi để tải tệp lên máy chủ. Nhưng tôi nhận được ngoại lệ mạng ngay cả sau nhiều lần thử và thậm chí sau khi thêm chế độ nghiêm ngặt.Tôi làm cách nào để sử dụng Tác vụ không đồng bộ để tải tệp lên máy chủ?
Tôi mới sử dụng Android và không biết làm cách nào tôi có thể sử dụng tác vụ không đồng bộ, mà nhiều người đã khuyên dùng cho loại hoạt động mạng như vậy. Bất cứ ai có thể cho tôi biết rằng tôi là sai trong mã và nơi tôi nên sử dụng công việc async?
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();
}
}
này không thể đọc được. Vui lòng định dạng mã của bạn bằng cách thụt lề bằng bốn dấu cách. – aliteralmind