Android 简单模仿AsyncHttpClient

使用方法

AsyncHttpClient ahc = new AsyncHttpClient();
ahc.get(mPath,mhandler);
private Handler mHandler = new Handler(){
    public void handleMessage(Message msg){
      swith(msg.what){

      }
    }
}
public class AsyncHttpClient {

    public void get(String path, final MyHandler handler){
        final Message message = new Message();
        new Thread(){
            @Override
            public void run() {
                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet();
                    HttpResponse response = client.execute(httpGet);
                    InputStream stream = response.getEntity().getContent();
                    if (response.getStatusLine().getStatusCode() == 200) {

                        message.what = MyHandler.SUCCESS;
                        message.obj = stream;
                        handler.sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    message.what = MyHandler.FAILURE;
                    handler.sendMessage(message);
                }
            }
        }.start();
    }
}

你可能感兴趣的:(android,anync)