Copyright (c) 2012, Snakk! Media Group
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are m...
If you think the Android project snakk-ads-android-sample-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.snakk.vastsdk;
/*www.java2s.com*/import android.os.AsyncTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
publicclass TVASTPostbackTask extends AsyncTask<Integer, Integer, Object> {
/**
* Callbacks for TVASTPostbackTask.
*/publicinterface TVASTPostbackListener {
/**
* This event is fired after AlertAd is loaded and ready to be displayed.
*/publicvoid onSuccess(String data);
/**
* This event is fired after AlertAd is displayed.
*/publicvoid onFailure(Exception error);
}
private String mUrl;
private TVASTPostbackListener mListener;
public TVASTPostbackListener getListener() {
return mListener;
}
publicvoid setListener(TVASTPostbackListener listener) {
mListener = listener;
}
private String postbackRequest(String url) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, 8192);
String responseValue = readInputStream(bufferedInputStream);
bufferedInputStream.close();
inputStream.close();
return responseValue;
}
privatestatic String readInputStream(BufferedInputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] buffer = newbyte[8192];
for (int n; (n = in.read(buffer)) != -1; ) {
out.append(new String(buffer, 0, n));
}
return out.toString();
}
public TVASTPostbackTask(String url) {
mUrl = url;
}
@Override
protected Object doInBackground(Integer... params) {
if (mUrl == null)
return null;
//Log.d("SnakkVASTSDK", mUrl);
String data;
try {
data = postbackRequest(mUrl);
return data;
} catch (IOException e) {
return e;
}
}
@Override
protectedvoid onPostExecute(Object response) {
//Log.d("SnakkVASTSDK", response);
String error = null;
if (response instanceof String) {
try {
JSONObject jsonObject = new JSONObject((String) response);
if (jsonObject.has("error")) {
error = jsonObject.getString("error");
Exception e = new Exception(error);
if (mListener != null) {
mListener.onFailure(e);
}
} else {
if (mListener != null) {
mListener.onSuccess((String) response);
}
}
} catch (JSONException jsonE) {
if (mListener != null) {
mListener.onSuccess((String) response);
}
}
} elseif (response instanceof Exception) {
if (mListener != null) {
mListener.onFailure((Exception) response);
}
}
}
}