Java BackCall 的返回值运行在新的Thread上。
所以,在Android 上不能咋 BackCall上更新 主线程。
https://paiza.io/projects/dBt6EFZZnt8GdS0MDW8I-g
import java.io.*;
import java.net.*;
public class Main {
public static void main(String[] args) throws Exception {
// Here your code !
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
Handel h = new Handel();
System.out.println("Call by BackCall thread name >>" + Thread.currentThread().getName());
h.Down(new Handel.BackCall() {
public void restout(String xx) {
System.out.println("BackCall thread name >>" + Thread.currentThread().getName());
System.out.println(xx);
}
});
}
}
class Handel {
public interface BackCall {
public void restout (String json);
}
public void Down(BackCall bc) {
new Thread(new Runnable() {
public void run() {
try {
URL u = new URL("http://www.yahoo.co.jp");
HttpURLConnection c = (HttpURLConnection)u.openConnection();
c.setConnectTimeout(3000);
c.setReadTimeout(3000);
c.setDoInput(true);
c.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040225 Firefox/0.8");
InputStream is = null;
c.setRequestMethod("GET");
String bbb = "";
if (c.getResponseCode() == 200) {
is = c.getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader(is));
bbb = br.readLine();
}
bc.restout(bbb);
} catch (Exception e) {
e.printStackTrace();
}
}
} ).start();
}
}
0 件のコメント:
コメントを投稿