主线程1执行//onPreExecute在主线程中执行命令 可有可无 通常做进度条的初始化
@Override
protected void onPreExecute() {
super.onPreExecute();
}
子线程2执行//doInBackground在子线程中执行命令
@Override
protected String doInBackground(String... params) {
HttpURLConnection con=null;
InputStream is=null;
StringBuilder sbd=new StringBuilder();
try {
URL url=new URL(params[0]);
con= (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
con.setReadTimeout(5*1000);
if (con.getResponseCode()==200){
is=con.getInputStream();
int next=0;
byte[] bt=new byte[1024];
while ((next=is.read(bt))>0){
sbd.append(new String(bt,0,next));
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace(); }}
if (con!=null){
con.disconnect();} }
return sbd.toString();}
调用publishProgress(progress。。。)被onProgress
主线程3//onPostExecute在UI线程中执行
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
show.setText(s);
}