2014-12-02 25 views
2

的廣告信息這是我們從Mopub和其他廣告網絡看到:廣告SDK似乎無法獲取設備

產生java.io.IOException:連接失敗 com.google.android。 gms.ads.identifier.AdvertisingIdClient.g(未知 來源) com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(未知 來源)

他們都似乎有同樣的問題。

奇怪的是,我們沒有任何問題從我們的應用獲取廣告ID與以下來源。我們得到正確的廣告ID,並且我們沒有錯誤日誌。 所有的SDK都遇到同樣的問題(連接失敗)。

任何幫助表示讚賞。

private void getAdvertisingId(AdvertisingIdHolder receiver) { 

    AdvertisingIdClient.Info adInfo = null; 
    String id = null; 
    boolean isLAT = false; 

    try { 
     adInfo = AdvertisingIdClient.getAdvertisingIdInfo(App.getCtx()); 

     id = adInfo.getId(); 
     isLAT = adInfo.isLimitAdTrackingEnabled(); 
    } catch (IOException e) { 
     SLog.e("error", e); 
     // Unrecoverable error connecting to Google Play services (e.g., 
     // the old version of the service doesn't support getting AdvertisingId). 
    } catch (GooglePlayServicesNotAvailableException e) { 
     SLog.e("error", e); 
     // Google Play services is not available entirely. 
    } catch (GooglePlayServicesRepairableException e) { 
     e.printStackTrace(); 
    } 

    receiver.receive(id, isLAT); 
} 

回答

1

這段日子我經歷了試驗和錯誤,獲取廣告ID。最後我得到了它! 如果我們傳入getApplicationContext()而不是當前活動的上下文,則可以解決連接錯誤。下面是我的工作代碼:

private void getGaid() { 
    new Thread(new Runnable() { 

     @Override 
     public void run() { 
      try { 
       String gaid = AdvertisingIdClient.getAdvertisingIdInfo(
         getApplicationContext()).getId(); 
       if (gaid != null) { 
        Log.d("DEBUG", gaid); 
        // gaid get! 
       } 
      } catch (IllegalStateException e) { 
       e.printStackTrace(); 
      } catch (GooglePlayServicesRepairableException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (GooglePlayServicesNotAvailableException e) { 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 
} 

getGaid()可放入的onCreate()的onResume(),或視圖的onClick(),只要該線程的主UI線程調用。

您可能需要的另一件事是更新谷歌播放服務庫到最新版本。正如文中提到的here,IOException可能是由於舊版本的服務不支持獲取AdvertisingId而導致的。

隨意評論,如果有任何其他問題。