Android通过读取网上的配置显示和隐藏广告

使用HttpClient读取GitHub上一个地址来判断是否显示或隐藏广告。
request.setHeader(“Cache-Control”, “no-cache”);不使用缓存。DefaultHttpClient默认缓存网页的数据。
在onResume()方法中调用getOnlineConfig()

private Handler mHttpHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "msg = " + msg);
            if (msg.what == 1) {
                mCanShowAd = true;
                showAds();
            } else {
                mCanShowAd = false;
                hideAds();
            }
        }

    };

    void getOnlineConfig() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                Looper.prepare();
                HttpGet request = new HttpGet("https://raw.githubusercontent.com/skyhacker2/FilpBlocks/master/ad.txt");
                request.setHeader("Cache-Control", "no-cache");
                HttpClient client = new DefaultHttpClient();
                try {
                    HttpResponse response = client.execute(request);
                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        HttpEntity entity = response.getEntity();
                        String content = new String(EntityUtils.toString(entity));
                        Log.d(TAG, "配置内容: " + content);
                        if (content.equals("true")){
                            mHttpHandler.sendEmptyMessage(1);
                        } else {
                            mHttpHandler.sendEmptyMessage(0);
                        }
                    }
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }

你可能感兴趣的:(Android通过读取网上的配置显示和隐藏广告)