java - Get loading percent from InputStream .read method -


i have problem in parsing inputstream , getting loading percentage of data. mean, method needs parse inputstream, put stringbuffer, total of bytes parsed , returns string based on stringbuffer.

private string processpercent(inputstream content, httpresponse response) throws ioexception {     inputstream in = content;     int totalbytes  = integer.parseint(response.getfirstheader("content-length").getvalue());     int processedbyte;     int loaded = 0;     stringbuffer sb = new stringbuffer();      while((processedbyte = in.read()) != -1)     {          sb.append((char) processedbyte);          if(this.asynctask instanceof iprogresspercent)         {             lastprocessed = processedbyte;             loaded += processedbyte;             float percent = ((100*loaded) / totalbytes);             this.progresspercent = (int)percent;             this.asynctask.doprogress(this.progresspercent);         }     }      in.close();      return new string(sb); } 

the problem when display value of percent variable, value higher 100, think that's calculation problem.

any idea ? thanks.

replace

    loaded += processedbyte; 

with

++loaded; 

before if actually; incrementing 1.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -