Android HttpUrlConnection not working in release

39 Views Asked by At

I'm trying to get String from Web API. String is a list of file names, separate with new line and fill List with those line strings. The method is working in debug mode on emulator, works in debugging on smart phone but when I generate release sign APK and install it on phone it doesn't work. It just hit catch block with exception message=null, response code =0, response message="", value of getconnectTimeout() and error log is writing:

02-11-2023 10:55:38 CET
null 0  15000
_____________________________________________

Any ideas or suggestions?

public class MbtilesFileNamesFromServer  {

    private String url;

    public MbtilesFileNamesFromServer(String url) {
        this.url = url;
    }

    protected List<String> GetFileNames() {
        HttpURLConnection urlConnection = null;

        List<String> fileNamesArray = new ArrayList<>();
        int code = 0;
        String response="";
        try {
            URL url = new URL(this.url);

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setConnectTimeout(15000);
            urlConnection.setReadTimeout(10000);
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoInput(true);
            urlConnection.connect();
            code = urlConnection.getResponseCode();
            response=urlConnection.getResponseMessage();

            if (code == 200) {
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                if (in != null) {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                    String line = "";

                    while ((line = bufferedReader.readLine()) != null)
                        fileNamesArray.add(line);

                }
                in.close();
            }
            return fileNamesArray;

        } catch (Exception e) {


            AppendErrLog(e.getMessage() + " " + String.valueOf(code) +" "+response+" "+ String.valueOf(urlConnection.getConnectTimeout()));
            urlConnection.disconnect();
        } finally {
            urlConnection.disconnect();
        }
        return fileNamesArray;
    }
0

There are 0 best solutions below