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();
    }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。