用线程Thread执行一些方法后,需要判断执行是否成功。
public void run() {}
run( ) 方法返回值是空, 怎么办?
解决方法:
- Note
- 使用 call() 方法
- Callable接口是 jdk 5 后新增的接口
代码:
package com.example.thread;import 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.StreamTool.StreamTool;public class MyThread1 implements Callable<String> { private static final String tag = "xxxyyy"; private String phone; private String name; public MyThread1(String name, String phone) { this.name = name; this.phone = phone; } // public MyThread1(String name, String phone) {// super(name);// this.phone = phone;// } // public void run() {// Log.i(tag, Thread.currentThread().getName() + "......start");// String newpath = "http:///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")) return "200"; } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO Auto-generated method stub return "404"; } }处理返回值
以上就是本文的全部内容,希望对大家的学习有所帮助。