groupdocs-conversion-cloud / groupdocs-conversion-cloud-android

Android module for communicating with the GroupDocs.Conversion Cloud REST API. Convert Documents, Images, Spreadsheets, PDF, Drawings, Emails and many other files in the Cloud.
https://products.groupdocs.cloud/conversion/android
MIT License
0 stars 0 forks source link

NetworkOnMainThreadException #2

Closed dineshchavhan closed 3 years ago

dineshchavhan commented 3 years ago

code is

    val MyClientId = "_provided_"
    val MyClientSecret = "_provided_"

    val configuration = Configuration(MyClientId, MyClientSecret)
    val apiInstance = ConvertApi(configuration)

    val settings = ConvertSettings()
    settings.filePath = getFilePath(uri)
    settings.format = "pdf"
    settings.outputPath = out

    val result = apiInstance.convertDocument(ConvertDocumentRequest(settings))

error occur Caused by: android.os.NetworkOnMainThreadException

SergeiTerentev commented 3 years ago

Hi, the NetworkOnMainThreadException occurs when an application attempts to perform a networking operation on its main thread, so for using GroupDocs API calls, an async task should be used, for example: ` @SuppressLint("StaticFieldLeak") private class GetDataTask extends AsyncTask<Void, Void, Boolean> { @Override protected Boolean doInBackground(Void... params) {

        String appSid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        String appKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

        Configuration configuration = new Configuration(appSid, appKey);

        InfoApi infoApi = new InfoApi(configuration);

        try {
            FormatsResult response = infoApi.getSupportedFileFormats();
            for (Format format : response.getFormats()) {
                System.out.println(format.getFileFormat());
            }
            return true;
        } catch (ApiException e) {
            System.err.println("Failed to get supported file formats");
            e.printStackTrace();
            return false;
        }

    }

    @Override
    protected void onPostExecute(final Boolean success) {
        mTask = null;
    }

    @Override
    protected void onCancelled() {
        mTask = null;
    }
}

`