0

i have a function fetch were i open a new thread and make a request to https://google.com but returns garbage

fetch function:
    public void fetch(View v) {
        fetchedData.clear();
        TextView label = findViewById(R.id.Hello_World_Label_1); // a label in the GUI
        Thread fetchThread = new Thread(new CustomRunnable(v, label) {
            @Override
            public void run() {
                try {
                    URL url = new URL("https://instagram.com");//StrUrl);
                    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                    conn.setSSLSocketFactory((SSLSocketFactory)HttpsURLConnection.getDefaultSSLSocketFactory());
                    conn.setHostnameVerifier(new HostnameVerifier() {
                        @Override
                        public boolean verify(String hostname, SSLSession session) {
                            return HttpsURLConnection.getDefaultHostnameVerifier().verify(hostname, session);
                        }
                    });
                    conn.connect();
                    BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
                    if (conn.getResponseCode() == 200) {
                        errorOccured = false;
                        byte[] barr = new byte[bis.available()];
                        bis.read(barr);
                        String[] str = (barr.toString()).split("\n");
                        fetchedData.addAll(Arrays.asList(str));
                    } else {
                        errorOccured = true;
                    }
                    bis.close();
                    conn.disconnect();
                } catch (java.net.MalformedURLException err) {
                    Log.d("ERROR_","MUE" + err.getMessage());
                } catch (java.io.IOException err) {
                    Log.d("ERROR_","IO: " + err.getMessage());
                }
            }
        });
        fetchThread.start();
    }

the CustomRunnable class is just a wrapper around Runnable and just overrides the constructor to be able to pass parameters

i tried makeing the request on https://instagram.com and about the same garbage got returned.

for the minimal executable example i am adding the get_line() function and the CustomRunnable:

    public void get_line(View v) {
        TextView label = (TextView)findViewById(R.id.Hello_World_Label_1); // a label in the GUI
        if (fetchedData.size() != 0 && !errorOccured) {
            label.setText(fetchedData.get(0));
            fetchedData.remove(0);
        } else {
            label.setText("[No more data]");
        }
    }
public class CustomRunnable implements Runnable {
    public CustomRunnable(View view, TextView label_) {
        View v = view;
        TextView label = label_;
    }

    @Override
    public void run() {}
}

NOTE: in MainActivity.java some variables:

private final List<Integer> grayButtons = new ArrayList<>();
private final List<String> fetchedData = new ArrayList<>();
private boolean errorOccured = false;

i am new to java and android development so please be patient with me

EXAMPLE OF THE PROBLEM: the returned data is like the following: [B@c940d29 and when i recall the get_line() i get to the [No more data]

  • `https://instagram.com` is redirected to `https://www.instagram.com/` but the web site seem to be generated on client side using JavaScript. And `[B@c940d29` is just a the hex code of on Java object that has no `toString()` implementation (like an array). – Robert May 15 '21 at 18:33
  • @Robert yeah but still you would get the basic HTML that has the scripts right? – user23441235234 May 15 '21 at 18:34
  • Yes, but you do not convert a `byte[]` to a `String` representation of those bytes by calling `toString()` on the `byte[]`. That will give you `[B@...]` for varying values of `...`. The simplest solution, by far, is to use a modern HTTP client API, such as OkHttp, where all of this code can be replaced by [stuff that is much shorter](https://square.github.io/okhttp/recipes/#synchronous-get-kt-java). – CommonsWare May 15 '21 at 18:37
  • Additionally your way of reading data is not working correctly. You have to call `read` multiple times and check the response value how many bytes have been read actually. See here for a correct example how to read String data from an `InputStream`: https://stackoverflow.com/a/40241635/150978 – Robert May 15 '21 at 18:38
  • See also https://stackoverflow.com/q/4328711/115145, if you really want to stick with `HttpsURLConnection` for some reason. – CommonsWare May 15 '21 at 18:39
  • Thank you all for the help. i managed to use your information to make it work. it was my first android app i ever made and i am really motivated to make more (and learn more). again thanks – user23441235234 May 15 '21 at 20:08
  • The way you are using `available()` is specifically warned *against* in the Javadoc. You must read the input stream in a loop until end of stream or an exception occurs. – user207421 May 15 '21 at 22:14

0 Answers0