我想上传一些存储在 android 设备上的文件,但它似乎卡在 AsyncTask 的 doInBackground 方法中。我没有错误。对话框弹出并保持活动状态我消除了对话框并且没有效果。我项目的另一部分是解码我上传的 json 文件并将它们存储在数据库中,但那是为了以后。
/********************UPLOAD GPSDATA*************************************/
class UploadGpsData extends AsyncTask<Void, Void, Void>{
NetworkInfo net;
MainActivity uActivity;
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String folderPath;
String arrayOfFiles[];
File root;
File allFiles;
String urlServer = "http://urluploadscriptaddress.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
URL url;
ProgressDialog pDialog = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
Log.d(" UploadGpsData","onPreRequest");
pDialog.setMessage("Uploading GPS Data. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
Log.d(" UploadGpsData","doInBackground");
root = Environment.getExternalStorageDirectory();
//pathToOurFile = root.getAbsolutePath()+"/Beagle Data/07-09-2013_16-21-30.json";
folderPath = root.getAbsolutePath()+"/Beagle Data/";
allFiles = new File(folderPath);
arrayOfFiles = allFiles.list();
for(int i = 0; i < arrayOfFiles.length; i++){
Log.d("File Names", arrayOfFiles[i].toString());
//File filename = new File(arrayOfFiles[i].toString());
try {
url = new URL(urlServer);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
try {
connection.setRequestMethod("POST");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
try{
FileInputStream fileInputStream = new FileInputStream(new File(folderPath+arrayOfFiles[i].toString()) );
try {
outputStream = new DataOutputStream( connection.getOutputStream() );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + folderPath+arrayOfFiles[i].toString() +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0){
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
//int serverResponseCode = connection.getResponseCode();
//String serverResponseMessage = connection.getResponseMessage();
// Responses from the server (code and message)
//serverResponseCode = connection.getResponseCode();
//serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
} catch(Exception e){
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute() {
Log.d(" UploadGpsData","onPost");
pDialog.dismiss();
txtUploadStatus = (TextView) findViewById(id.txtUploadStatus);
txtUploadStatus.setText("Upload Achieved");
}
}
/********************END OF UPLOADGPSDATA*************************************/
下面的 PHP 脚本:错误日志是空的,所以我假设它卡在 android 应用程序上
<?php
$target_path = "./";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$file = basename( $_FILES['uploadedfile']['name']);
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)
?>