コード例 #1
0
  private LoadUrlTaskResult loadAdFromNetwork(String url) throws Exception {
    HttpGet httpget = new HttpGet(url);
    httpget.addHeader("User-Agent", mUserAgent);

    HttpClient httpclient = getAdViewHttpClient();
    mResponse = httpclient.execute(httpget);
    HttpEntity entity = mResponse.getEntity();

    // Anything but a 200 OK is an invalid response.
    if (mResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK
        || entity == null
        || entity.getContentLength() == 0) {
      throw new Exception("MoPub server returned invalid response.");
    }

    // Ensure that the ad type header is valid and not "clear".
    Header atHeader = mResponse.getFirstHeader("X-Adtype");
    if (atHeader == null || atHeader.getValue().equals("clear")) {
      throw new Exception("MoPub server returned no ad.");
    }

    configureAdViewUsingHeadersFromHttpResponse(mResponse);

    // Handle custom event ad type.
    if (atHeader.getValue().equals("custom")) {
      Log.i("MoPub", "Performing custom event.");
      Header cmHeader = mResponse.getFirstHeader("X-Customselector");
      mIsLoading = false;
      return new PerformCustomEventTaskResult(cmHeader);
    }
    // Handle native SDK ad type.
    else if (!atHeader.getValue().equals("html")) {
      Log.i("MoPub", "Loading native ad");
      Header npHeader = mResponse.getFirstHeader("X-Nativeparams");
      if (npHeader != null) {
        mIsLoading = false;
        HashMap<String, String> paramsHash = new HashMap<String, String>();
        paramsHash.put("X-Adtype", atHeader.getValue());
        paramsHash.put("X-Nativeparams", npHeader.getValue());
        Header ftHeader = mResponse.getFirstHeader("X-Fulladtype");
        if (ftHeader != null) paramsHash.put("X-Fulladtype", ftHeader.getValue());
        return new LoadNativeAdTaskResult(paramsHash);
      } else throw new Exception("Could not load native ad; MoPub provided no parameters.");
    }

    // Handle HTML ad.
    InputStream is = entity.getContent();
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    for (int n; (n = is.read(b)) != -1; ) {
      out.append(new String(b, 0, n));
    }
    return new LoadHtmlAdTaskResult(out.toString());
  }
コード例 #2
0
ファイル: AdFetcher.java プロジェクト: gnr/mopub-client
    private AdFetchResult fetch(String url) throws Exception {
      HttpGet httpget = new HttpGet(url);
      httpget.addHeader("User-Agent", mAdFetcher.mUserAgent);

      // We check to see if this AsyncTask was cancelled, as per
      // http://developer.android.com/reference/android/os/AsyncTask.html
      if (isCancelled()) {
        mFetchStatus = FetchStatus.FETCH_CANCELLED;
        return null;
      }

      if (mAdView == null || mAdView.isDestroyed()) {
        Log.d("MoPub", "Error loading ad: AdView has already been GCed or destroyed.");
        return null;
      }

      HttpResponse response = mHttpClient.execute(httpget);
      HttpEntity entity = response.getEntity();

      if (response == null || entity == null) {
        Log.d("MoPub", "MoPub server returned null response.");
        mFetchStatus = FetchStatus.INVALID_SERVER_RESPONSE_NOBACKOFF;
        return null;
      }

      final int statusCode = response.getStatusLine().getStatusCode();

      // Client and Server HTTP errors should result in an exponential backoff
      if (statusCode >= 400) {
        Log.d(
            "MoPub",
            "Server error: returned HTTP status code "
                + Integer.toString(statusCode)
                + ". Please try again.");
        mFetchStatus = FetchStatus.INVALID_SERVER_RESPONSE_BACKOFF;
        return null;
      }
      // Other non-200 HTTP status codes should still fail
      else if (statusCode != HttpStatus.SC_OK) {
        Log.d(
            "MoPub",
            "MoPub server returned invalid response: HTTP status code "
                + Integer.toString(statusCode)
                + ".");
        mFetchStatus = FetchStatus.INVALID_SERVER_RESPONSE_NOBACKOFF;
        return null;
      }

      mAdView.configureAdViewUsingHeadersFromHttpResponse(response);

      // Ensure that the ad is not warming up.
      Header warmupHeader = response.getFirstHeader("X-Warmup");
      if (warmupHeader != null && warmupHeader.getValue().equals("1")) {
        Log.d(
            "MoPub",
            "Ad Unit ("
                + mAdView.getAdUnitId()
                + ") is still warming up. "
                + "Please try again in a few minutes.");
        mFetchStatus = FetchStatus.AD_WARMING_UP;
        return null;
      }

      // Ensure that the ad type header is valid and not "clear".
      Header atHeader = response.getFirstHeader("X-Adtype");
      if (atHeader == null || atHeader.getValue().equals("clear")) {
        Log.d("MoPub", "No inventory found for adunit (" + mAdView.getAdUnitId() + ").");
        mFetchStatus = FetchStatus.CLEAR_AD_TYPE;
        return null;
      }

      // Handle custom native ad type.
      else if (atHeader.getValue().equals("custom")) {
        Log.i("MoPub", "Performing custom event.");

        // If applicable, try to invoke the new custom event system (which uses custom classes)
        Header customEventClassNameHeader = response.getFirstHeader("X-Custom-Event-Class-Name");
        if (customEventClassNameHeader != null) {
          Map<String, String> paramsMap = new HashMap<String, String>();
          paramsMap.put("X-Custom-Event-Class-Name", customEventClassNameHeader.getValue());

          Header customEventClassDataHeader = response.getFirstHeader("X-Custom-Event-Class-Data");
          if (customEventClassDataHeader != null) {
            paramsMap.put("X-Custom-Event-Class-Data", customEventClassDataHeader.getValue());
          }

          return new PerformCustomEventTaskResult(mAdView, paramsMap);
        }

        // Otherwise, use the (deprecated) legacy custom event system for older clients
        Header oldCustomEventHeader = response.getFirstHeader("X-Customselector");
        return new PerformLegacyCustomEventTaskResult(mAdView, oldCustomEventHeader);

      }

      // Handle mraid ad type.
      else if (atHeader.getValue().equals("mraid")) {
        Log.i("MoPub", "Loading mraid ad");
        Map<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put("X-Adtype", atHeader.getValue());

        String data = httpEntityToString(entity);
        paramsMap.put("X-Nativeparams", data);
        return new LoadNativeAdTaskResult(mAdView, paramsMap);

      }

      // Handle native SDK ad type.
      else if (!atHeader.getValue().equals("html")) {
        Log.i("MoPub", "Loading native ad");

        Map<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put("X-Adtype", atHeader.getValue());

        Header npHeader = response.getFirstHeader("X-Nativeparams");
        paramsMap.put("X-Nativeparams", "{}");
        if (npHeader != null) {
          paramsMap.put("X-Nativeparams", npHeader.getValue());
        }

        Header ftHeader = response.getFirstHeader("X-Fulladtype");
        if (ftHeader != null) {
          paramsMap.put("X-Fulladtype", ftHeader.getValue());
        }

        return new LoadNativeAdTaskResult(mAdView, paramsMap);
      }

      // Handle HTML ad.
      String data = httpEntityToString(entity);
      return new LoadHtmlAdTaskResult(mAdView, data);
    }