【问题标题】:Google Analytics API V3 Read TimeOut ErrorGoogle Analytics API V3 读取超时错误
【发布时间】:2017-09-19 23:29:23
【问题描述】:

有时我在执行 Google Analytics API v3 时收到以下错误消息

错误:

java.net.SocketTimeoutException: 读取超时 在 java.net.SocketInputStream.socketRead0(本机方法) 在 java.net.SocketInputStream.socketRead(未知来源) 在 java.net.SocketInputStream.read(未知来源) 在 java.net.SocketInputStream.read(未知来源) 在 sun.security.ssl.InputRecord.readFully(未知来源

基本上,我正在尝试执行以下代码:

代码:

Get get =  analytics.data().ga().get(bean.getIds(), bean.getStartDate(), bean.getEndDate(), bean.getMetrics());
    get.setDimensions(bean.getDimensions());
    get.setSamplingLevel(bean.getSamplingLeve());
    get.setMaxResults(bean.getMaxResult());
    query.setIds("ga:"+ids[i]);
    get.buildHttpRequest().setReadTimeout(5 * 60000);
    get.buildHttpRequest().setConnectTimeout(5 * 60000);
    gaList.add(get.execute());

但是 setReadTimeout(millisec) 和 serConnectionTimeout(millisec) 不起作用。

有什么帮助吗?

【问题讨论】:

    标签: java google-analytics-api


    【解决方案1】:

    试试这个并从你的代码中删除 setTimout 代码: Java Google Analytics API - Read timed out

        private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
          return new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest httpRequest) throws IOException {
              requestInitializer.initialize(httpRequest);
              httpRequest.setConnectTimeout(3 * 60000);  // 3 minutes connect timeout
              httpRequest.setReadTimeout(3 * 60000);  // 3 minutes read timeout
            }};
    
     }
    public static Analytics initializeAnalytics() throws Exception {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_LOCATION))
            .setServiceAccountScopes(AnalyticsScopes.all())
            .build();
    
        // Construct the Analytics service object.
        return new Analytics.Builder(httpTransport, JSON_FACTORY,setHttpTimeout(credential))
            .setApplicationName(APPLICATION_NAME).build();
      }
    

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多