2015-07-21 88 views
1

有時我執行谷歌分析API V3時收到以下錯誤消息谷歌Analytics(分析)API V3讀取超時錯誤

錯誤:

java.net.SocketTimeoutException:閱讀是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) 在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(毫秒)和serConnectionTimeout(毫秒)不起作用。

任何幫助?

回答

0

從你試試這個,取出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(); 
    }