Example #1
0
  public static AndroidHttpClient getHttpClient() {
    String userAgent = DeviceUtils.getUserAgent();

    AndroidHttpClient httpClient = AndroidHttpClient.newInstance(userAgent);

    HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
    HttpClientParams.setRedirecting(params, true);

    return httpClient;
  }
  public void makeRequest(
      @Nullable final RequestParameters requestParameters, @Nullable Integer sequenceNumber) {
    final Context context = getContextOrDestroy();
    if (context == null) {
      return;
    }

    if (!DeviceUtils.isNetworkAvailable(context)) {
      mMoPubNativeNetworkListener.onNativeFail(CONNECTION_ERROR);
      return;
    }

    loadNativeAd(requestParameters, sequenceNumber);
  }
 @VisibleForTesting
 void onAdError(@NonNull final VolleyError volleyError) {
   MoPubLog.d("Native ad request failed.", volleyError);
   if (volleyError instanceof MoPubNetworkError) {
     MoPubNetworkError error = (MoPubNetworkError) volleyError;
     switch (error.getReason()) {
       case BAD_BODY:
         mMoPubNativeNetworkListener.onNativeFail(INVALID_RESPONSE);
         return;
       case BAD_HEADER_DATA:
         mMoPubNativeNetworkListener.onNativeFail(INVALID_RESPONSE);
         return;
       case WARMING_UP:
         // Used for the sample app to signal a toast.
         // This is not customer-facing except in the sample app.
         MoPubLog.c(MoPubErrorCode.WARMUP.toString());
         mMoPubNativeNetworkListener.onNativeFail(EMPTY_AD_RESPONSE);
         return;
       case NO_FILL:
         mMoPubNativeNetworkListener.onNativeFail(EMPTY_AD_RESPONSE);
         return;
       case UNSPECIFIED:
       default:
         mMoPubNativeNetworkListener.onNativeFail(UNSPECIFIED);
         return;
     }
   } else {
     // Process our other status code errors.
     NetworkResponse response = volleyError.networkResponse;
     if (response != null && response.statusCode >= 500 && response.statusCode < 600) {
       mMoPubNativeNetworkListener.onNativeFail(SERVER_ERROR_RESPONSE_CODE);
     } else if (response == null && !DeviceUtils.isNetworkAvailable(mContext.get())) {
       MoPubLog.c(String.valueOf(MoPubErrorCode.NO_CONNECTION.toString()));
       mMoPubNativeNetworkListener.onNativeFail(CONNECTION_ERROR);
     } else {
       mMoPubNativeNetworkListener.onNativeFail(UNSPECIFIED);
     }
   }
 }