httpReturn = "";//clear global var MyThread thread = new MyThread("http://rockplay.sinaapp.com/kofi_api.php?number=" + number); thread.start(); // wait for result for (int i=0;i<3*10;i++){ try { Thread.sleep(100); if (httpReturn.length()>0) break; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
// 停顿后做的操作写这里
}
};
timer.schedule(task, 1000 * 1); //停顿1秒
private int getCallInNumberType(String number){
class MyThread extends Thread {
private String url;
public MyThread(String url)
{
this.url = url;
}
@Override
public void run() {
//httpReturn = MainActivity.getApi2(this.url);
try {
URL url = new URL(this.url);
try
{
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3*1000);
if (conn.getResponseCode() == HttpStatus.SC_OK){
InputStream is = conn.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
httpReturn = br.readLine();
}
conn.disconnect();
}catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
httpReturn = "";//clear global var
MyThread thread = new MyThread("http://rockplay.sinaapp.com/kofi_api.php?number=" + number);
thread.start();
// wait for result
for (int i=0;i<3*10;i++){
try {
Thread.sleep(100);
if (httpReturn.length()>0) break;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if ((httpReturn.length()>0) && (Character.isDigit(number.charAt(0)))){
return httpReturn.charAt(0) - (int)('0');
}
return 0;
}