三星设备上的 http 连接问题,但其他设备上没有

Issue with http connection on samsung device but not on other device

我有一个 android 应用程序,它在我的 phone(Huawei Mate 10 Pro)上完美运行,但是当我尝试在我的平板电脑(Samsung Galaxy Tab S2)上运行它时当应用程序尝试访问输入流时,我得到一个 java.io.FileNotFoundException。

(我在 "InputStream stream = connection.getInputStream();" 处得到异常)

三星和其他设备在http通信方面有区别吗?如何编写代码以在所有设备上运行?

代码如下:

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;
}

这是 StackTrace(替换了我的 url):

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.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)

当它启动 http 通信时,我也在调试器控制台中得到了这个:

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

这相关吗?

经过多次试验和错误后,我仍然不知道问题出在哪里,但我尝试使用 class OkHttp 而不是 HttpUrlConnection,突然一切正常。我不知道为什么,但是因为我现在有一个工作代码,所以我不再需要解决这个问题的帮助。我希望这个解决方案(完全改变 class)可以帮助将来的人。有关我使用的 class 的更多信息,请参见此处:http://square.github.io/okhttp/