2

sometimes I am getting the following error message when executing Google Analytics API v3

Error :

java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at sun.security.ssl.InputRecord.readFully(Unknown Source

Basically, I am trying to execute the following code:

Code :

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());

But the setReadTimeout(millisec) and serConnectionTimeout(millisec) are not working.

Any help?

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
Fabio Zioli
  • 143
  • 1
  • 3
  • 15

1 Answers1

1

Try this and remove setTimout codes from yours: 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();
  }
Utku Soytaş
  • 1,475
  • 2
  • 19
  • 30