android - How to return a value from thread in java? -
in android creating thread url connection.inside thread store response message in string globally declared.when access method method returns null.
public class rate_fetch { string total=""; public string rate(string dt) { new thread(new runnable(){ public void run(){ try { url url = new url(tally_ipaddr+"/prorate.jsp?plist="+sss.tostring().trim()); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); inputstream in = new bufferedinputstream(urlconnection.getinputstream()); bufferedreader r = new bufferedreader(new inputstreamreader(in)); string x = ""; string total = ""; x = r.readline(); int i=0; while(x.length()>1) { total=total+x.tostring().trim(); i++; x = r.readline(); } } catch(exception e){ return e.tostring(); } } }).start(); return total; }
when call method returns null.
rate_fetch rf=new rate_fetch(); string amt= rf.rate(prodlist);
it want use thread
only, try this
public class rate_fetch { string total = ""; boolean b = true; public string rate(string dt) { stringbuilder sb = new stringbuilder(); new thread(new runnable() { public void run() { try { url url = new url(tally_ipaddr + "/prorate.jsp?plist=" + sss.tostring().trim()); httpurlconnection urlconnection = (httpurlconnection) url .openconnection(); inputstream in = new bufferedinputstream(urlconnection .getinputstream()); bufferedreader r = new bufferedreader( new inputstreamreader(in)); stringbuilder sb = new stringbuilder(); string s; while (true) { s = r.readline(); if (s == null || s.length() == 0) break; sb.append(s); } b = true; } catch (exception e) { b = true; } } }).start(); while (b) { } total = sb.tostring(); return sb.tostring(); } }
Comments
Post a Comment