hexols
hexols

Reputation: 13

NanoHTTPD Timeout Error while Uploading File to the Android Device

I am new to AOSP. NanoHttpd is implemented for web part of the project. I want to upload file from a Javascript page which is hosted on the Android device and then I want to get this uploaded file to the Android Device's file system. I am facing some issues:

    private Response handlePostMethod(IHTTPSession session) {
    final Map<String, String> params = new HashMap<>();
    try {
        session.parseBody(params);
    } catch (IOException e) {
        return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, e.getMessage());
    } catch (ResponseException e) {
        return new Response(e.getStatus(), MIME_PLAINTEXT, e.getMessage());
    }


    if (params.containsKey("postData") ||  session.getUri().equalsIgnoreCase("/upload")) {
        final String postData = params.get("postData");
        Logd("data " + postData);

        switch (session.getUri()) {
            ...
            case "/upload":
            Log.d(TAG,"upload case entered");
                try {

                    session.parseBody(params);
                    for (String key : params.keySet()) {
                        File file1 = new File(params.get(key));
                        if(file1==null){
                            Log.d(TAG,"inside for file1 is empty");
                        }

                        byte[] fileData = Files.readAllBytes(file1.toPath());

                        ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData);
                        String fileDownloadLocation = Environment.getExternalStorageDirectory() + "/Download";
                        File newFile = new File(fileDownloadLocation);
                        return responseOk;
                        //  downloadFiles(newFile,session);

                    }
                } catch (Exception e) {
                    Logd("responseJsonError:" + e);
                    return responseJsonError;
                }
        }
    }

    return null;
}

and I am posting the file from javascript :

    apkInstallBtn.onclick = () => {
    // Get the file input element
    var fileInput = document.getElementById('fileInput');

    // Check if a file is selected
   

    // Create a URL for the file object
    var fileURL = URL.createObjectURL(file);
    fileURL = fileURL.replace("blob:","");
    
    
    // Log the URL of the file
    console.log('File URL:', fileURL);

    // Create a FormData object
    var formData = new FormData();
    formData.append('file', file);

    // Make a POST request to upload the file
    fetch('http://' + ip_address + '/upload', {
        method: 'POST',
        body: formData
    })
    .then(response => response.text())
    .then(data => console.log(data))
    .catch(error => console.error('Error:', error));
    


};

It gives this error:

User Error while uploading file: java.net.SocketTimeoutException: Read timed out on line session.parseBody(files);

How can I fix this?

I tried to set timeout value but It didn't work either.

session.setReadTimeout(timeoutInMillis);

It gave this error:

symbol: method setReadTimeout(int) location: variable session of type IHTTPSession

Upvotes: 0

Views: 76

Answers (0)

Related Questions