簡體   English   中英

android,上傳文件時異步任務出錯?

[英]Error in async task while uploading file, android?

這是我將圖像上傳到服務器的異步任務代碼,從圖庫中選擇圖像

  private class uploadFileAsync extends AsyncTask<String[],Void, Void>{

    ProgressDialog dialog = new ProgressDialog(CrimeReportActivity.this);

    @Override
        protected void onPreExecute() {
                // update the UI immediately after the task is executed
        super.onPreExecute();
        dialog.setMessage("Please wait...");
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.show();
        }

        @Override
        protected Void doInBackground(String[]... params) {


            String fileUploadResp=  fs.fileUpload(filePath, uploadURL);
            System.out.println(fileUploadResp);
            return null;
        }


        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
             dialog.dismiss();

        }
 }

這是我的上載課

    public class FileShare {
@SuppressWarnings("deprecation")
public String fileUpload(String videoPath, String uploadURL) {

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;


    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 10 * 1024 * 1024;
    try {
        // ------------------ CLIENT REQUEST
        FileInputStream fileInputStream = new FileInputStream(new File(
                videoPath));
        // open a URL connection to the Servlet
        URL url = new URL(uploadURL);
        // Open a HTTP connection to the URL
        conn = (HttpURLConnection) url.openConnection();
        // Allow Inputs
        conn.setDoInput(true);
        // Allow Outputs
        conn.setDoOutput(true);
        // Don't use a cached copy.
        conn.setUseCaches(false);
        // Use a post method.
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);
        dos = new DataOutputStream(conn.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                + videoPath + "\"" + lineEnd);
        dos.writeBytes(lineEnd);
        // create a buffer of maximum size
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        // read file and write it into form...
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        while (bytesRead > 0) {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }
        // send multipart form data necesssary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        // close streams
        Log.e("Debug", "File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();
    } catch (MalformedURLException ex) {
        Log.e("Debug", "error: " + ex.getMessage(), ex);
    } catch (IOException ioe) {
        Log.e("Debug", "error: " + ioe.getMessage(), ioe);
    }
    // ------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream(conn.getInputStream());

        while ((str = inStream.readLine()) != null) {
            Log.e("Debug", "Server Response " + str);
        }
        inStream.close();
    } catch (IOException ioex) {
        Log.e("Debug", "error: " + ioex.getMessage(), ioex);
    }
    return str;
}

這是logcat

     FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
   at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
at    com.pstpl.crimeverify.CrimeReportActivity$uploadFileAsync.doInBackground(CrimeReportActivity.java:376)
at com.pstpl.crimeverify.CrimeReportActivity$uploadFileAsync.doInBackground(CrimeReportActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more

我正在嘗試在doinbackground中獲取serverresponse字符串。 知道我做錯了什么嗎?

解決了問題。 上課似乎是錯誤的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM