使用AsyncTask Android展示广告

时间:2013-05-27 12:03:40

标签: android android-asynctask http-get

Android 2.3.3

我正在将AD服务集成到我的客户端应用程序中。

以下是我在AsyncTask中所做的事情:

  1. 构造一个查询并使用HTTP Get方法查询具有所需宽度和高度以及其他参数的服务器。
  2. 从服务器获得响应后,我必须在主线程上更新它(将响应附加为HTML标记)。
  3. 问题在于AD随机显示。这与网络问题有关还是与我的代码有关?而且,如果使用DefaultHTTPClient和其他任何东西一样好吗?

    主要活动中的代码:

    private static GetMMediaAdsTask mGetMMediaAds;
    
    static String responseBody = "";
    
    StringBuffer html = new StringBuffer();
    ....
    ....
    html.append("<p>");
    String url = "http://xxxx.com/getAd";
    
    mGetMMediaAds = new GetMMediaAdsTask(context);
    mGetMMediaAds.execute(url); // Calling the execute method of asynctask
    
    ...
    ...
    ...
    html.append(responseBody);
    html.append("</p>");
    

    AsyncTask SubClass

    public static class GetMMediaAdsTask extends AsyncTask<String, Void, Boolean>{
    
        String url = "http://ads.mp.mydas.mobi/getAd";
        String apid = "123xxx";
        String auid = "";
    
        String returned = "";
    
        private Context mContext;
    
    
        public GetMMediaAdsTask(Context context) {
            // TODO Auto-generated constructor stub
            mContext = context;
        }
    
    
        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
    
            System.out.println("***** Inside AsyncTask *****");
    
            auid = Secure.getString(mContext.getContentResolver(), Secure.ANDROID_ID);
    
            int hsht = 0;
            int hswd = 0;
    
            DisplayMetrics metrics = new DisplayMetrics();
            ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
            int height = metrics.heightPixels;
            int width = metrics.widthPixels;
    
            System.out.println("Height ="+height+" Width = "+width);
    
            if(width <= 320)
            {
                hswd = 320;
                hsht = 53;
            }
            else if(width > 320 && width < 468)
            {
                hswd = 320;
                hsht = 53;
            }
            else if(width >= 468 && width < 728)
            {
                hswd = 468;
                hsht = 60;
            }
            else if(width >= 728)
            {
                hswd = 728;
                hsht = 90;
            }
    
            String query = String.format("apid=%s&auid=%s&hsht=%d&hswd=%d", apid, auid, hsht, hswd);
            System.out.println("query = "+query);
    
            try {
                DefaultHttpClient http = new DefaultHttpClient();
                HttpGet httpMethod = new HttpGet();
                httpMethod.setURI(new URI(url + "?" + query));
                HttpResponse response = http.execute(httpMethod);
                int responseCode = response.getStatusLine().getStatusCode();
                switch (responseCode) {
                case 200:
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        returned = EntityUtils.toString(entity);
                        System.out.println("response = "+returned);
    
                    }
                    break;
                }
    
            } catch (Exception e) {
                System.out.println("Exception occured");
            }
    
    
    
            return true;
        }
    
        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
    
            System.out.println("***** Inside PostExecute *****");
            responseBody = returned;
    
            super.onPostExecute(result);
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

AsyncTask和UI线程是并行执行的,所以当你执行html.append(responseBody);时,可能还没有设置responseBody。

将基于AsyncTask结果更新UI的代码移动到onPostExecute - 保证在doInBackground完成后在UI线程上调用它

而不是静态returned,最好将其设为AsyncTask结果