这是我们从Mopub和其他广告网络中看到的内容:
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);
}
答案 0 :(得分:2)
这些天我通过试验和错误获得广告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线程调用该线程。
您可能需要的另一件事是将Google Play服务库更新为最新版本。正如官方文档here所提到的,IOException可能是因为旧版本的服务不支持获取AdvertisingId。
如果有任何其他问题,请随时发表评论。