主线程当您在Android的网络连接,你是阻塞主线程,所以你必须移动这个任务的“一些”到一个新的线程其操作正好堵在android系统
我有2个在此部分
1-其以下的动作的阻止主线程(A或B)
//A:
HttpURLConnection c = (HttpURLConnection) (new URL(url)).openConnection();
//B:
InputStream stream=c.getInputStream();
2-“两者”上述(A & B)的问题,如果必须在运行新的线程,它有一个坏的影响,运行在一个新的独立的每一个读?看看下面的代码:
//I temporary removed try & catch to simplify the code
public class connect{
HttpURLConnection c; String url;
public connect(String url){
this.url=url;
new Thread(new Runnable{
@override public void run(){
c = (HttpURLConnection) (new URL(url)).openConnection();
}
});
}
public InputStream get(){
return c.getInputStream();
//or make this one in a new thread
}
public InputStream post(Sring params){
c.setRequestMethod("POST");
//.. make some code for posting data , and then call get()
//thats why i cannot perform c.getInputStram() at the same time with openConnection()
return get()
}
}
我想避免的AsyncTask –
好吧,明天我会送一个示例代码与处理程序和尺蠖工作。 –
好的,我在等,先谢谢你 –