【问题标题】:Display Ads using AsyncTask Android使用 AsyncTask Android 展示广告
【发布时间】:2013-05-27 12:03:40
【问题描述】:

Android 2.3.3

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

这是我在 AsyncTask 中所做的:

  1. 构造一个查询并使用 HTTP Get 方法以所需的宽度和高度以及其他参数查询服务器。
  2. 从服务器获得响应后,我必须在主线程上更新它(将响应附加为 HTML 标记)。

问题是广告是随机显示的。这与网络问题有关还是与我的代码有关?另外,如果使用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 子类

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

}

【问题讨论】:

  • 在 onPostExecute 中设置 responseBody = returned 但您还必须让 Activity 知道何时执行任务。那就是调用你的 Activity 的一个方法并将响应体传递给它,然后在这个方法中组装你的 html 页面。
  • 问题是,html 是使用字符串缓冲区构建的,并且在底部(就在附加

标签: android android-asynctask http-get


【解决方案1】:

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

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

而不是静态的returned,最好让它成为AsyncTask结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 2018-06-23
    • 2018-02-01
    • 1970-01-01
    相关资源
    最近更新 更多