i got a java method which returns the response of the website in a string. Now i want to add the possibility to track the progress of this request. I know i can calculate it via (contenLength/readBytes) *100. But i am not sure how to retrieve this information properly and update the progress everytime it changes. My current method looks like this:
public String executePost(URL url) {
StringBuffer sb = new StringBuffer();
int readBytes = 0;
int contentLength = 0;
int progress = 0;
try {
String newString = url.toString().replace(" ", "%20");
URL newURL = new URL(newString);
URLConnection conn = newURL.openConnection();
conn.setDoOutput(true);
contentLength = conn.contentLength();
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
readBytes += line.getBytes("ISO-8859-2").length + 2;
progress = (readBytes/contentLength)*100;
System.out.println(progress);
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}