Después de ejecutar algunos métodos con Thread Thread, debe determinar si la ejecución es exitosa.
public void run () {}
El valor de retorno del método run () está vacío, ¿qué debo hacer?
Solución:
Código:
paquete com.example.thread; import java.io.ioException; import java.io.inputstream; import java.net.httpurlConnection; import java.net.malFormedureLEctional; import java.net.rurl; import java.util.concurrent.callable; import android.util.log; import La clase MyThread1 implementa Callable <String> {private static final string tag = "xxxyyy"; Teléfono de cadena privada; nombre de cadena privada; public mythread1 (nombre de cadena, teléfono de cadena) {this.name = name; this.phone = teléfono; } // public mythread1 (nombre de cadena, teléfono de cadena) {// super (nombre); // this.phone = phone; //} // public void run () {// log.i (tag, thread.currentThread (). getName () + "...... inicio"); // string newpath = "http://www.dianping.com/ajax/json/account/reg/mobile/send?m=" + phone + "& flow = t & callback = dp._jsonprequest._5"; // stringbuilder strbui = nuevo stringBuilder (newpath); /// thin {// url = neweNe Url (strbui.ToString ()); // httpurlconnection con = (httpurlconnection) url.openconnection (); // con.addrequestProperty ("content-type", "text/html; charset = utf-8"); // con.addrequestproperty (///"usuarios; usuarios; "Mozilla/5.0 (Windows NT 6.1) AppleWebkit/537.36 (khtml, como gecko) Chrome/38.0.2125.104 Safari/537.36"); // Con.addrequestProperty ("Cookie",,,, "_hc.v =/" /// "0ffcdf91-a4d6-4b08-8800-3d59d2e2fe45.1413899372 ////"/"; abtest =/" 48,124 // | 52,133 // | 47,122 // | 44,106 // | 45,115/"; __utma = 1.29319792.1412090870.1413609669.1413645970.5; _TR.U = 7DHY0YFHD9RY065F; 13.T = 17DEDA7D2A7D8E46D84BF12A74627B72; _hc.v =/"///" 1bdad871-405e-46e3-97d6-46571ad48e14.1413897928 /// "/"; JSessionID = 891671fc6b450418dd6e0611a3e4cf48 "); // con.addrequestProperty (" referente "," http://t.dianping.com/register") ;// con.setconnecttimeOut (5000); // con.setrequmestm ("get"); (con.getResponseCode () == 200) {// inputStream inputStr = con.getInputStream (); // string info = new String (streamTool.read (inputStr), "utf-8"); // log.i (etiqueta, s. Bloque de captura generado automático // E.PrintStackTrace (); //} Catch (IOException e) {// // // tODO Auto Generated Catch Block // E.PrintStackTrace (); //} Catch (IOException e) {// // TODO Auto Generated Catch Block //pintstacktrace (); ///} Catch (excepción (excepción (excepción) TODO Bloque de captura generada automáticamente // E.PrintStackTrace (); //} // //} @Override public String Call () lanza excepción {log.i (tag, thread.currentThread (). GetName () + "...... inicio"); String newpath = "http://www.dianping.com/ajax/json/account/reg/mobile/send?m=" + teléfono + "& flow = t & callback = dp._jsonprequest._5"; StringBuilder strBui = new StringBuilder (NewPath); intente {url url = new url (strbui.toString ()); Httpurlconnection con = (httpurlconnection) url.openconnection (); con.AddRequestProperty ("Content-type", "Text/html; charset = utf-8"); Con.AddRequestProperty ("User-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebkit/537.36 (khtml, como gecko) Chrome/38.0.2125.104 Safari/537.36"); con.addrequestProperty ("Cookie", "_hc.v =/" /// "0ffcdf91-a4d6-4b08-8800-3d59d2e2fe45.1413899372 ////"/"; Abtest =/"48,124 // | 52,133 // | 47,122 // | 44,106 // | 45,115/"; __utmz = 1.1413645970.5.20 T_RCT = 2172824 | 2171729 | 6457678; 13.ts = CD3BD6495620E2A204912C4BDFD43220; _hc.v =/"//" 1bdad871-405e-46e3-97d6-46571ad48e14.1413897928 /// "/"; JSessionID = 891671fc6b450418dd6e0611a3e4cf48 "); Con.addrequestProperty ("Referente", "http://t.dianping.com/register"); con.setConnectTimeOut (5000); con.setRequestMethod ("Get"); flotante x = 3.4f; if (con.getResponseCode () == 200) {inputStream inputStr = con.getInputStream (); String info = new String (streamtool.read (inputStr), "UTF-8"); Log.i (etiqueta, thread.currentThread (). GetName () + info); if (info.contains ("200")) return "200"; }} Catch (MalFormedUrePleCeption e) {// TODO BLOCK AUTO GENERADO E.PRINTSTACKTRACE (); } Catch (ioException e) {// tODO Auto Generated BLOCK E.PrintStackTRace (); } Catch (Exception e) {// TODO Auto Generado Bloque E.PrintStackTrace (); } // TODO Método auto-generado retorno "404"; }} Procesar el valor de retorno
EjecutorService exec = Ejecutors.NewCachedThreadPool (); ArrayList <Future <String>> resultados = new ArrayList <Future <String>> (); para (int i = 0; i <num; i ++) results.Add (exec.submit (selectThread (TelNumber))); para (Future <String> fs: resultados) try {if (fs.get (). Equals ("200")) Success ++; } Catch (InterruptedException e) {// TODO Auto Generado Bloque E.PrintStackTrace (); } Catch (ExecutionException e) {// TODO Auto Generado Bloque E.PrintStackTrace (); } return éxito;Lo anterior se trata de este artículo, espero que sea útil para el aprendizaje de todos.