Reputation: 13
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]
Upvotes: 0
Views: 84