Reprinted from http://bbs.csdn.net/topics/390731832
Problem:
Public StaticString getaddress (FinalInputStream InputStream,FinalString Mobile) { NewThread () { Public voidrun () {Try{log.i (TAG,"InputStream:" +inputstream.available ()); String Soap=Readsoapfile (InputStream, mobile); byte[] data =soap.getbytes (); URL URL=NewURL ("Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); HttpURLConnection Conn=(httpurlconnection) URL. OpenConnection (); Conn.setdooutput (true);//set Allow outputConn.setconnecttimeout (5 * 1000);//set the time-out to 5 secondsConn.setrequestmethod ("POST"); Conn.setrequestproperty ("Content-type", "Application/soap+xml; Charset=utf-8 "); Conn.setrequestproperty ("Content-length", String.valueof (data.length)); //Set LengthOutputStream OutputStream=Conn.getoutputstream (); Outputstream.write (data); Outputstream.flush (); Outputstream.close (); if(Conn.getresponsecode () = = 200) {Address=Parseresponsexml (Conn. getInputStream ());//parse the data returned by the server side}System.out.println ("address==" +address);//here is a value } } Catch(Exception e) {log.e (TAG, e.tostring ()); System.out.println ("Get Phone number attribution failed"); } }; }.start (); System.out.println ("Addres" +address);//The return here is empty. returnaddress; }
addess return null value
Because it is not in a thread, the value cannot be passed across threads, the workaround:
Public StaticString getaddress (FinalInputStream InputStream,FinalString Mobile) {Thread Thread=NewThread () { Public voidrun () {Try{Log. I (TAG,"InputStream:" +inputstream.available ()); String Soap=Readsoapfile (InputStream, mobile); byte[] data =soap.getbytes (); URL URL=NewURL ("Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx" ); HttpURLConnection Conn=(httpurlconnection) URL. OpenConnection (); Conn.setdooutput (true); Conn.setconnecttimeout (5 * 1000); Conn.setrequestmethod ("POST"); Conn.setrequestproperty ("Content-type", "Application/soap+xml; Charset=utf-8 "); Conn.setrequestproperty ("Content-length", String. ValueOf (data.length)); OutputStream OutputStream=Conn.getoutputstream (); Outputstream.write (data); Outputstream.flush (); Outputstream.close (); if(Conn.getresponsecode () = = 200) {Address=Parseresponsexml (Conn. getInputStream ()); } } Catch(Exception e) {}}; }; Thread.Start (); try {thread.join ();} catch (Exception e) {} if (address!=null) {return address; } return NULL ; }
How multi-Threading is implemented in the business code (Java Class) and returns the value of the child thread with the method