0

我想上传一些存储在 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)
?>
4

3 回答 3

0

你错过了paramsforonPostExecute()可能是一个问题

    @Override
    protected void onPostExecute(Void result) {

即使你没有从你那里返回任何东西,doInBackground()你仍然需要 this 作为AsyncTask方法。如果这不能解决它,那么还请发布你是如何调用它的,并解释你是否在任何地方设置了断点以查看它在做什么。

于 2013-07-11T18:44:32.383 回答
0

您没有实现@Override onPostExecute。

用这个修改你的 onPostExecute 分配:

@Override
protected void onPostExecute(Void result) {

这将解决您的问题。发生了什么:您没有覆盖 OnPostExecute AsyncTask 方法,而是创建了一个新方法。

于 2013-07-11T18:57:57.530 回答
-1

您在非 UI 线程中“操纵”UI。

尝试注释所有与 UI 相关的代码,它应该可以工作。

您可以使用

Context.runOnUiThread(new Runnable() ....

为了做UI的东西。

于 2014-06-09T13:31:22.840 回答