0

I am trying to make users send some app related files to me. I made a "file request" folder in my drop box page for that. Every time i get this message Error 405 - Method not allowed. Here is my code:

private class UploadFile extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        try {
            String sourceFileUri = "/data/com.mostafa.android.roadbump/databases/matab.db";
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;

            if (dB.isFile()) {

                try {
                    String upLoadServerUri = "https://www.dropbox.com/request/KJcdVMDyxHvM2So1mJkK";

                    // open a URL connection to the Server
                    FileInputStream fileInputStream = new FileInputStream(dB);
                    URL url = new URL(upLoadServerUri);

                    // Open a HTTP connection to the URL
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true); // Allow Inputs
                    conn.setDoOutput(true); // Allow Outputs
                    conn.setUseCaches(false); // Don't use a Cached Copy
                    conn.setRequestMethod("PUT");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                    conn.setRequestProperty("bill", sourceFileUri);

                    dos = new DataOutputStream(conn.getOutputStream());

                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
                            + sourceFileUri + "\"" + 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);

                    Log.d("Sasaaa", "Done");

                    int responseCode = conn.getResponseCode();
                    String responseMessage = conn.getResponseMessage();

                    Log.d("Sasaaa", String.valueOf(responseCode));
                    Log.d("Sasaaa", responseMessage);

                    // close the streams //
                    conn.disconnect();
                    fileInputStream.close();
                    dos.flush();
                    dos.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
Greg
  • 16,359
  • 2
  • 34
  • 44
  • Dropbox doesn't offer a programmatic interface for uploading to file requests like this. We'll consider it a feature request. Programmatic uploading should be done via the API, as noted in the answer below. – Greg Aug 25 '16 at 18:22

1 Answers1

0

Error 405 - Method not allowed tells me that your request type is not correct. You're trying to execute a PUT request in your server. Please check that again, the server request type might be a POST.

Another thing you need to check is the url or the path you're calling the server method. Incorrect url may result this type of errors.

The last thing you need to check if there's any kind of authentication process before you call the method.

Edit

Dropbox has their own API to upload files to Dropbox server. You might check for them to get this job done easily.

I'm quoting another answer from here for your easy access.

private DropboxAPI<AndroidAuthSession> mDBApi;

File tmpFile = new File(fullPath, "/data/com.mostafa.android.roadbump/databases/matab.db);

FileInputStream fis = new FileInputStream(tmpFile);

try {
    DropboxAPI.Entry newEntry = mDBApi.putFileOverwrite("matab.db", fis, tmpFile.length(), null);
} catch (DropboxUnlinkedException e) {
    Log.e("DbExampleLog", "User has unlinked.");
} catch (DropboxException e) {
    Log.e("DbExampleLog", "Something went wrong while uploading.");
}

You might take a look at here to get idea of API Key and APP SECRET.

Community
  • 1
  • 1
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Thank you for your response :) .. I used POST earlier and it gave me the same response, so i tried PUT as alternative .. The url is correct and i double checked it with DROPBOX to be sure .. I don't know about the authentication process, so if anyone can enlighten us i really will appreciate it. – Mostafa Mohammed Aug 21 '16 at 12:45
  • I know about the DropBox API but i was searching for an easier way to do the task specially that they have already opened 'File Requests'. – Mostafa Mohammed Aug 25 '16 at 02:43