How to pause a Httpurlconnection in android on button click

Viewed 488

I am entirely new to android development and I have been sending a large file through httpurlconnection in android. The code i used for uploading files through httpurlconnection is

 try {
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));

    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setChunkedStreamingMode(1024)

    // Enable POST method
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

    outputStream = new DataOutputStream(connection.getOutputStream());
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);

    // TOKENID
    String token = "anyvalye";
    outputStream.writeBytes("Content-Disposition: form-data; name=\"Token\"" + lineEnd);
    outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
    outputStream.writeBytes("Content-Length: " + token.length() + lineEnd);
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(token + lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);

    String connstr = null;
    connstr = "Content-Disposition: form-data; name=\"UploadFile\";filename=\""
            + pathToOurFile + "\"" + lineEnd;

    outputStream.writeBytes(connstr);
    outputStream.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    TotalFileSize=bufferSize;

    System.out.println("Initial Max Size" + bytesAvailable + "");
    buffer = new byte[bufferSize];

    // Read file
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    try {
        while (bytesRead > 0) {
            try {
                outputStream.write(buffer, 0, bufferSize);
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
                response = "outofmemoryerror";
                return response;
            }
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        }
    } catch (Exception e) {
        e.printStackTrace();
        response = "error";
        return response;
    }
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    int serverResponseCode = connection.getResponseCode();
    String serverResponseMessage = connection.getResponseMessage();
    System.out.println("Server Response Code " + " " + serverResponseCode);
    System.out.println("Server Response Message "+ serverResponseMessage);

    if (serverResponseCode == 200) {
        response = "true";
    } else {
        response = "false";
    }

    fileInputStream.close();
    outputStream.flush();

    connection.getInputStream();
    //for android InputStream is = connection.getInputStream();
    java.io.InputStream is = connection.getInputStream();

    int ch;
    StringBuffer b = new StringBuffer();
    while((ch = is.read()) != -1 ) {
        b.append((char)ch);
    }

    String responseString = b.toString();
    System.out.println("response string is" + responseString); //Here is the actual output

    outputStream.close();
    outputStream = null;

} catch (Exception ex) {
    // Exception handling
    response = "error";
    System.out.println("Send file Exception" + ex.getMessage() + "");
    ex.printStackTrace();
}
return response;

I came to know that during downloading files, we can pause and resume the download and there are a lot of codes available. I tried that codes and also got the solution.

But can i pause and resume the upload process in Httpurlconnection. Forgive me if my question is silly because i am newly learning this concepts. Can anyone help me here for pausing and resuming upload since there are no codes available upto my knowledge.

After debugging, i am getting the response for size of file upload here at this piece of code,

bytesRead = fileInputStream.read(buffer, 0, bufferSize);
try {
    while (bytesRead > 0) {
        try {
            outputStream.write(buffer, 0, bufferSize);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            response = "outofmemoryerror";
            return response;
        }
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    }
} catch (Exception e) {
    e.printStackTrace();
    response = "error";
    return response;
}

Hoping to get some help to update myself.

1 Answers
```
 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            try {
                while (bytesRead > 0) {
                    try {
                        outputStream.write(buffer, 0, bufferSize);
                    } catch (OutOfMemoryError e) {
                        e.printStackTrace();
                        response = "outofmemoryerror";
                        return response;
                    }
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                }
            } catch (Exception e) {
                e.printStackTrace();
                response = "error";
                return response;
}
        }
    ```
Related