Android Open Source - playnomics-android Asset Client






From Project

Back to project page playnomics-android.

License

The source code is released under:

Apache License

If you think the Android project playnomics-android 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.playnomics.android.client;
/*from  w  w w .  j  a  v a  2  s .c o  m*/
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.TreeMap;

import com.playnomics.android.util.Logger;
import com.playnomics.android.util.Logger.LogLevel;

public class AssetClient {

  public enum ResponseStatus {
    SUCCESS, FAILURE
  }

  public class AssetResponse {

    private ResponseStatus status;
    private int responseCode;

    public int getResponseCode() {
      return responseCode;
    }

    public ResponseStatus getStatus() {
      return status;
    }

    private String requestUrl;

    public String getRequestUrl() {
      return requestUrl;
    }

    private Exception exception;

    public Exception getException() {
      return exception;
    }

    private byte[] data;

    public byte[] getData() {
      return data;
    }

    public AssetResponse(String requestUrl, Exception exception) {
      this.status = ResponseStatus.FAILURE;
      this.requestUrl = requestUrl;
      this.responseCode = 0;
    }

    public AssetResponse(String requestUrl, byte[] data) {
      this.status = ResponseStatus.SUCCESS;
      this.requestUrl = requestUrl;
      this.data = data;
      this.responseCode = 200;
    }

    public AssetResponse(String requestUrl, ResponseStatus status, int responseCode) {
      this.status = status;
      this.requestUrl = requestUrl;
      this.responseCode = responseCode;
    }
  }

  protected IHttpConnectionFactory connectionFactory;
  private Logger logger;
  
  public AssetClient(IHttpConnectionFactory connectionFactory, Logger logger) {
    this.connectionFactory = connectionFactory;
    this.logger = logger;
  }

  public AssetResponse requestAsset(String baseUrl, String path,
      TreeMap<String, Object> queryParameters) {
    String assetUrl = this.connectionFactory.buildUrl(baseUrl, path,
        queryParameters);
    return requestAsset(assetUrl);
  }

  public AssetResponse requestAsset(String url) {
    HttpURLConnection connection = null;
    BufferedInputStream bufferedIn = null;
    AssetResponse response;
    
    logger.log(LogLevel.DEBUG, "Requesting asset at %s", url);
    
    try {
      connection = connectionFactory.startConnectionForUrl(url);
      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
        response = new AssetResponse(url, ResponseStatus.FAILURE, connection.getResponseCode());
      } else {
        InputStream inputStream = connection.getInputStream();
        int bufferSize = 1024 * 4;// 4KB

        bufferedIn = new BufferedInputStream(inputStream, bufferSize);
        ByteArrayOutputStream bufferedOut = new ByteArrayOutputStream();
        int read;
        while ((read = bufferedIn.read()) != -1) {
          bufferedOut.write(read);
        }
        bufferedIn.close();

        byte[] data = bufferedOut.toByteArray();
        response = new AssetResponse(url, data);
      }
    } catch (IOException e) {
      logger.log(LogLevel.WARNING, "Could not retrieve asset at %s", url);
      response = new AssetResponse(url, e);
    } finally {
      if (connection != null) {
        connection.disconnect();
      }
    }
    return response;
  }
}




Java Source Code List

com.playnomics.PlaynomicsMoreTestActivity.java
com.playnomics.PlaynomicsTestAppActivity.java
com.playnomics.RichDataFrameDelegate.java
com.playnomics.android.client.AssetClient.java
com.playnomics.android.client.EventQueue.java
com.playnomics.android.client.EventWorker.java
com.playnomics.android.client.HttpConnectionFactory.java
com.playnomics.android.client.IEventQueue.java
com.playnomics.android.client.IEventWorker.java
com.playnomics.android.client.IHttpConnectionFactory.java
com.playnomics.android.client.PlacementDataClient.java
com.playnomics.android.client.SegmentationClient.java
com.playnomics.android.events.AppPageEvent.java
com.playnomics.android.events.AppPauseEvent.java
com.playnomics.android.events.AppResumeEvent.java
com.playnomics.android.events.AppRunningEvent.java
com.playnomics.android.events.AppStartEvent.java
com.playnomics.android.events.CustomEvent.java
com.playnomics.android.events.ExplicitEvent.java
com.playnomics.android.events.ImplicitEvent.java
com.playnomics.android.events.PlaynomicsEvent.java
com.playnomics.android.events.TransactionEvent.java
com.playnomics.android.events.UserInfoEvent.java
com.playnomics.android.messaging.CloseButton.java
com.playnomics.android.messaging.HtmlAdFactory.java
com.playnomics.android.messaging.HtmlAd.java
com.playnomics.android.messaging.HtmlCloseButton.java
com.playnomics.android.messaging.MessagingManager.java
com.playnomics.android.messaging.NativeCloseButton.java
com.playnomics.android.messaging.Placement.java
com.playnomics.android.messaging.Position.java
com.playnomics.android.messaging.Target.java
com.playnomics.android.messaging.ui.IPlayViewFactory.java
com.playnomics.android.messaging.ui.PlayDialog.java
com.playnomics.android.messaging.ui.PlayViewFactory.java
com.playnomics.android.messaging.ui.PlayWebView.java
com.playnomics.android.messaging.ui.RenderTaskFactory.java
com.playnomics.android.push.GcmBroadcastReceiver.java
com.playnomics.android.push.GcmIntentService.java
com.playnomics.android.push.GcmManager.java
com.playnomics.android.sdk.IGoogleCloudMessageConfig.java
com.playnomics.android.sdk.IPlacementDelegate.java
com.playnomics.android.sdk.IPlaynomicsPlacementDelegate.java
com.playnomics.android.sdk.IPlaynomicsPlacementRawDelegate.java
com.playnomics.android.sdk.IPlaynomicsSegmentationDelegate.java
com.playnomics.android.sdk.IPushConfig.java
com.playnomics.android.sdk.IPushNotificationDelegate.java
com.playnomics.android.sdk.Playnomics.java
com.playnomics.android.segments.UserSegmentIds.java
com.playnomics.android.session.ActivityObserver.java
com.playnomics.android.session.GameSessionInfo.java
com.playnomics.android.session.HeartBeatHandler.java
com.playnomics.android.session.HeartBeatProducer.java
com.playnomics.android.session.IActivityObserver.java
com.playnomics.android.session.ICallbackProcessor.java
com.playnomics.android.session.IHeartBeatProducer.java
com.playnomics.android.session.SessionStateMachine.java
com.playnomics.android.session.Session.java
com.playnomics.android.session.TouchEventHandler.java
com.playnomics.android.session.WindowCallbackProxy.java
com.playnomics.android.util.AndroidLogger.java
com.playnomics.android.util.AsyncTaskRunner.java
com.playnomics.android.util.CacheFile.java
com.playnomics.android.util.Config.java
com.playnomics.android.util.ContextWrapper.java
com.playnomics.android.util.EventTime.java
com.playnomics.android.util.IAsyncCall.java
com.playnomics.android.util.IConfig.java
com.playnomics.android.util.IRandomGenerator.java
com.playnomics.android.util.LargeGeneratedId.java
com.playnomics.android.util.LogWriter.java
com.playnomics.android.util.Logger.java
com.playnomics.android.util.Util.java