アンドロイド 2.3.3
HTTP Get リクエストを使用して、サーバーから (AD) Script/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>");
何か必要な場合はお知らせください...ありがとう