Après avoir exécuté certaines méthodes avec Thread Thread, vous devez déterminer si l'exécution est réussie.
public void run () {}
La valeur de retour de la méthode run () est vide, que dois-je faire?
Solution:
Code:
package com.example.thread; importer java.io.ioexception; import java.io.inputStream; import java.net.httpurlconnection; import java.net.malformedUrlexception; import java.net.url; import java.util.concurrent.callable; import android.util.log; import com.example. class myThread1 implémente callable <string> {private static final string tag = "xxxyyy"; téléphone de chaîne privé; nom de chaîne privé; public myThread1 (String Name, String Phone) {this.name = name; this.phone = téléphone; } // public myThread1 (String Name, String Phone) {// super (name); // this.phone = téléphone; //} // public void run () {// log.i (tag, thread.currentThread (). getName () + "...... start"); // string newpath = "http://www.dianping.com/ajax/json/Account/reg/mobile/send?m=" + téléphone + "& flow = t & callback = dp._jsonprequest._5"; // stringbuilder strBui = new StringBuilder (newPath); /// essai {// url = new = new Url (strbui.toString ()); // httpurlconnection con = (httpurlconnection) url.openconnection (); // con.addrequestproperty ("contenu-type", "text / html; charset = utf-8"); // con.adDrequestProperty (// ". "Mozilla / 5.0 (Windows NT 6.1) Applewebkit / 537.36 (khtml, comme 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 //112222222 __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 (" référer "," http://t.dianping.com/register") ;// con.setConnectTimeout (5000); // con.sertequestMethod ("Get"); (con.getResponSECODE () == 200) {// inputStream inputStr = con.getInputStream (); // String info = new String (streamtool.read (inputstr), "utf-8"); // log.i (tag, thread.currentThread (). getName () + info); //} //} catch (malform Bloc de catch généré automatique // e.printStackTrace (); //} catch (ioException e) {// // // TODO Generated Catch Block // E.PrintStackTrace (); //} Catch (ioException e) {// // toodo Generated Catch Block // ToDoDo (); // Bloc de catch généré automatique // e.printStackTrace (); //} // //} @Override public String Call () lève une exception {log.i (tag, thread.currentThread (). GetName () + "...... start"); String newpath = "http://www.dianping.com/ajax/json/Account/reg/mobile/send?m=" + téléphone + "& flow = t & callback = dp._jsonprequest._5"; StringBuilder strBui = new StringBuilder (newPath); essayez {url url = new url (strbui.toString ()); HttpurlConnection con = (httpurlconnection) url.openconnection (); con.addrequestProperty ("contenu-type", "text / html; charset = utf-8"); CON.AdDrequestProperty ("User-Agent", "Mozilla / 5.0 (Windows NT 6.1) Applewebkit / 537.36 (KHTML, comme 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 ("référente", "http://t.dianping.com/register"); con.setConnectTimeout (5000); con.setRequestMethod ("get"); float x = 3,4f; if (con.getResponSECODE () == 200) {inputStream inputStr = con.getInputStream (); String info = new String (streamtool.read (inputStr), "utf-8"); Log.i (tag, thread.currentThread (). GetName () + info); if (info.Contains ("200")) renvoie "200"; }} catch (MalformEdUrException e) {// Bloc de catch généré automatiquement de TODO E.PrintStackTrace (); } catch (ioException e) {// TODO Bloc de capture généré automatiquement e.printStackTrace (); } catch (exception e) {// TODO Bloc de capture généré automatiquement e.printStackTrace (); } // TODO Méthode générée automatique Stub Retour "404"; }} Traiter la valeur de retour
ExecutorService exec = exécutor.NewCachedThreadPool (); ArrayList <futur <string>> Results = new ArrayList <futur <string >> (); pour (int i = 0; i <num; i ++) results.Add (exec.submit (selectThread (telNumber))); pour (futur <string> fs: résultats) essayez {if (fs.get (). equals ("200")) Success ++; } Catch (InterruptedException e) {// TODO Block de catch généré automatiquement e.printStackTrace (); } Catch (EXECUTUTIONException E) {// TODO Génératif Auto Generated Block E.PrintStackTrace (); } Retour succès;Ce qui précède concerne cet article, j'espère qu'il sera utile à l'apprentissage de tout le monde.