IntroductioniVdopia's Rich ads can be served inside the applications without having the need to integrate native SDKs. This powerful feature is available by adding just a few lines of code inside the application.Please refer the following document to understand the meaning of different parameters that can be passed.SDK Free Integration Sample Code demonstrating the use of the adFetch API
(Adds a webview of size 320X75 to the top of the parent view ). The following section demonstrates how to create a webView using the layout XML. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <WebView android:id="@+id/webview" android:layout_width="320px" android:layout_height="75px" android:layout_alignParentTop="true"> </WebView> </RelativeLayout> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <WebView android:id="@+id/webview" android:layout_width="320px" android:layout_height="75px" android:layout_alignParentBottom="true"> </WebView> </RelativeLayout> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <WebView android:id="@+id/webview" android:layout_width="wrap_content" android:layout_height="wrap_content"> </WebView> </RelativeLayout> To add transparency and add other properties add the following section of code. // Get the handle to the webView webView = (WebView)findViewById(R.id.webview); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setBuiltInZoomControls(true); webView.setBackgroundColor(0); Sample Code demonstrating how to request a vdobanner of size 320 X 75 from the Vdopia Server.
URL requestURL = null;
String AD_FETCH_URI = new String("http://serve.vdopia.com/adserver/html5/adFetch/?output=xhtml&container=androidWeb"); public void constructRequest() { try { String strUrl = new String(AD_FETCH_URI); String adType = "vdobanner"; // Change this type according to the ad type required String key = "AX123"; // Your API key goes here String location = "locbot"; // Set locbot = 1, which means that the video banner will expand from bottom to top, setting it to 0 means the reverse.
String formatted = new String(String.format("%s&caller=vdosdk&adFormat=%s&ak=%s&version=1.0&fullscreen=1&showClose=0&vdo=1&locbot=1",strUrl,adType, key )); strUrl = new String(formatted);
// Create the URL try { requestURL = new URL(formatted); } catch ( MalformedURLException e) { System.out.println("Bad URL: " + requestURL); } } catch(Exception e) { System.out.println("Exception caught "+ e); }
}
import java.io.InputStream; import java.net.URL; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; public class VDOParser extends DefaultHandler { boolean boFetchAd; boolean boFoundAd;
String adData;
private AdParseEventListener adParseEventListener;
interface AdParseEventListener { public void adParseStatus(Boolean status, String adData); }
public void initParser(URL sourceURL, AdParseEventListener listener ) { this.adParseEventListener = listener; try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader();
xr.setContentHandler(this); xr.parse(new InputSource(sourceURL.openStream())); }catch (Exception e) { System.out.println("XML Parsing Exception in initParser = " + e); } }
public void startDocument() {
}
/** Called when tag starts */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if(localName.equals("xhtml")) { boFetchAd = true; adData = new String(); } } /** Called when tag closing */ @Override public void endElement(String uri, String localName, String qName) throws SAXException { if(localName.equals("xhtml")) { boFetchAd = false; boFoundAd = true; } } /** Called to get tag characters */ @Override public void characters(char[] ch, int start, int length) throws SAXException {
if(boFetchAd == true) { adData = adData.concat(new String(ch, start, length)); } }
public void endDocument() { adParseEventListener.adParseStatus(boFoundAd, adData); } }
import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.webkit.MimeTypeMap; import android.webkit.WebView; import android.webkit.WebViewClient; import com.vdopia.android.VDOParser.AdParseEventListener; public class SDKFreeActivity extends Activity implements AdParseEventListener{
WebView webView; VDOParser parser; URL requestURL = null;
String AD_FETCH_URI = new String("http://serve.vdopia.com/adserver/html5/adFetch/?output=xhtml&container=androidWeb");
static final String SCHEME_TALK2ME_REPLAY = "replay:"; static final String SCHEME_TALK2ME_GOTOAPP = "gotoapp:";
private Activity activity;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
// Storing the activity context activity = this; // Get the handle to the webView webView = (WebView)findViewById(R.id.webview);
if(webView != null) { webView.setWebViewClient(new WebViewClient() { public void onPageStarted (WebView view, String url, Bitmap favicon) { } public void onPageFinished(WebView view, String url) { } public boolean shouldOverrideUrlLoading(WebView view, String url) { if(url.startsWith(WebView.SCHEME_TEL)) { Intent dialer = Intent.createChooser(new Intent(Intent.ACTION_DIAL, Uri.parse(url)), "Choose Dialer"); activity.startActivity(dialer); } else if (url.startsWith(WebView.SCHEME_MAILTO)) { Intent mailer = Intent.createChooser(new Intent(Intent.ACTION_SENDTO, Uri.parse(url)), "Send Message"); activity.startActivity(mailer); } else if (url.startsWith(WebView.SCHEME_GEO)) { Intent geoviewer = Intent.createChooser(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), "Choose Viewer"); activity.startActivity(geoviewer); } else if(url.startsWith("sms")) { Intent smsIntent = new Intent(Intent.ACTION_VIEW); smsIntent.putExtra("address", url.substring(url.indexOf(":") + 1)); smsIntent.setType("vnd.android-dir/mms-sms"); activity.startActivity(smsIntent); }else { String extension = MimeTypeMap.getFileExtensionFromUrl(url); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); if(!handleMimeType(url, mimeType)){ view.loadUrl(url); } } return true; } });
webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setBuiltInZoomControls(true);
webView.setBackgroundColor(0);
// Create the URL request constructRequest();
// Initiate the parser Object parser = new VDOParser(); parser.initParser(requestURL, this); } }
// This is a callback method implemented to find out if ad was Found public void adParseStatus(Boolean status, String adData) { if((status == true) && (adData != null)) { webView.loadData(adData, "text/html", "UTF-8"); } else { // Resume normal application or show ads by another provider } }
private boolean handleMimeType(String u, String mimeType){ String url = u; if(mimeType != null && mimeType.startsWith("video/")) { Intent videoIntent = new Intent(Intent.ACTION_VIEW); videoIntent.setDataAndType(Uri.parse(url), mimeType); activity.startActivity(videoIntent); return true; } return false; } } Special URL HandlingIn order to ensure that all the links in the advertisements work properly, the function shouldOverrideUrlLoading should be implemented properly to handle the URLs. |