1

I have an android app which works perfectly on my phone (Huawei Mate 10 Pro) but when I try to run it on my tablet (Samsung Galaxy Tab S2) I get a java.io.FileNotFoundException when the app tries to access the input stream.

(I get the exception at "InputStream stream = connection.getInputStream();")

Is there a difference between Samsung and other devices regarding http communication? how can I write the code to work on every device?

Here's the code:

public String download_organisations(String url){
    String jsonString = "";
    try {
        if(!url.startsWith("http://")){
            url = "http://" + url;
        }
        if(url.endsWith("/")){
            url = url.substring(0, url.lastIndexOf("/"));
        }
        url = url + ManagerSystemStatic.URL_SERVICE_WEB_ORGANISATION_MANAGER;
        URL httpUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
        connection.setConnectTimeout(10000);

        InputStream stream = connection.getInputStream();

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[1024];
        while ((nRead = stream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();
        byte[] inputStreamByteArray = buffer.toByteArray();
        byte[] base64 = Base64.decode(inputStreamByteArray, 0);
        byte[] decrypted = CipherUtils.decrypt(base64);
        jsonString = new String(decrypted);
        stream.close();
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            Log.d("OrganisationManager", "Download succeded with response: " + connection.getResponseCode());
        } else {
            Log.d("OrganisationManager", "Download failed with response: " + connection.getResponseCode());
        }
    } catch (MalformedURLException e) {
        Log.e("OrganisationManager", e.toString());
        return DOWNLOAD_FAILED;
    } catch (IOException e) {
        Log.e("OrganisationManager", e.toString());
        e.printStackTrace();
        return DOWNLOAD_FAILED;
    } catch (Exception e) {
        Log.e("OrganisationManager", e.toString());
        return DOWNLOAD_FAILED;
    }

    return jsonString;
}

Here's the StackTrace (with my url replaced):

E/OrganisationManager: java.io.FileNotFoundException: http://www.myurlhere.com
W/System.err: java.io.FileNotFoundException: http://www.myurlhere.com
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:254)
W/System.err:     at com.organisationmanager.ble.common.WebPortalCommunicationHelper.download_organisations(WebPortalCommunicationHelper.java:210)
              at com.organisationmanager.ble.ScanningActivity$UpdateOrganisations.doInBackground(ScanningActivity.java:414)
              at com.organisationmanager.ble.ScanningActivity$UpdateOrganisations.doInBackground(ScanningActivity.java:403)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:304)
              at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
D/InputTransport: Input channel constructed: fd=82
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err:     at java.lang.Thread.run(Thread.java:762)

When it starts the http communication I also get this in the debugger console:

D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false

Is that relevant?

Kurodani
  • 115
  • 10

1 Answers1

1

After a lot of trial and error I still don't know what the issue was, but I tried using the class OkHttp instead of HttpUrlConnection, and suddenly everything worked. I have no idea why, but since I now have a working code I no longer need help with this issue. I hope this solution (changing class entirely) may help someone in the future. More about the class I used can be found here: http://square.github.io/okhttp/

Kurodani
  • 115
  • 10