安卓 2.3.3
我正在尝试使用 HTTP Get 请求并从服务器获取(AD)脚本/HTML。得到响应后,我试图将响应附加到 WebView。我每次都从服务器正确地得到响应。我得到 HTML 或脚本。但有时广告会显示,有时不会。请参考以下详情
日志猫...
05-31 15:36:33.339: E/Web Console(10861): Uncaught TypeError: Cannot call method 'setAttribute' of undefined at
http://lp.xxx.xx/rich/T/test_suite/smartphone/320x50_ex_launch.php?mm_view=js&mm_urid=ZyAOlvcAeXFxzt4seOQ0WWwL&mm_ipaddress=49.205.242.120
&mm_handset=3768&mm_carrier=0&mm_apid=xxxxx&mm_acid=465355&mm_osid=58
&mm_uip=49.205.242.120&mm_ua=Apache-HttpClient%2FUNAVAILABLE+%28java+1.4%29
&mm_mtpid=0&mm_hswd=320&mm_hsht=53
&mm_auid=mmid_11a58a0069120505278791da8d22d5ad28_013ec7373911
&mm_country=100&mm_orut=1369994770&mm_mmid=3768:495
Java 代码 - AsyncTask - 工作正常
public static class GetMMediaAdsTask extends AsyncTask<String, Void, Boolean>{
String url = "http://ads.mp.xxx.xx/getAd";
String apid = "xxx";
String auid = "";
String returned = "";
private Context mContext;
private ProgressDialog progressDialog;
public GetMMediaAdsTask(Context context) {
// TODO Auto-generated constructor stub
mContext = context;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progressDialog = new ProgressDialog(mContext);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Loading...");
progressDialog.show();
super.onPreExecute();
}
@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();
System.out.println("Response Code = "+ responseCode);
switch (responseCode) {
case 200:
HttpEntity entity = response.getEntity();
if (entity != null) {
returned = "";
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;
System.out.println("responseBody = "+responseBody);
mViewPager.setAdapter(mArticlePagerAdapter);
mViewPager.setCurrentItem(mPosition);
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
setShareIntent(position);
if (mOnPageSelectedListener != null)
mOnPageSelectedListener.onPageSelected(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
progressDialog.dismiss();
super.onPostExecute(result);
}
}
MainActivity 中的代码
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>");
请让我知道,如果你需要什么...谢谢