使用Google Publishing API上传AAB文件时出现SocketTimeoutException

时间:2019-06-17 20:12:58

标签: java android google-play android-app-bundle

我正在尝试使用Google Publishing API上传AAB文件。以下是发布功能。

public void publish() throws Exception {
    String packageName = "myapp.package.name";
    String applicationName = "myApp";
    Path jsonKey = Paths.get("directory path", "secret.json");
    GoogleCredential cred = GoogleCredential.fromStream(new FileInputStream(jsonKey.toFile()));
    cred = cred.createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));
    com.google.api.services.androidpublisher.AndroidPublisher.Builder ab = new com.google.api.services.androidpublisher.AndroidPublisher.Builder(cred.getTransport(), cred.getJsonFactory(), cred);
    com.google.api.services.androidpublisher.AndroidPublisher publisher = ab.setApplicationName(applicationName).build();
    AppEdit edit = publisher.edits().insert(packageName, null).execute();
    final String editId = edit.getId();
    Path file = Paths.get("directory", "myApp.aab");
    AbstractInputStreamContent aabContent = new FileContent("application/octet-stream", file.toFile());
    Bundle bundle = publisher.edits().bundles().upload(packageName, editId, aabContent).execute();
}

我在最后一行收到以下读取超时异常:

java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347)
    at com.google.api.client.http.javanet.NetHttpResponse.<init>(NetHttpResponse.java:36)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:144)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:79)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:995)
    at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithoutGZip(MediaHttpUploader.java:548)
    at com.google.api.client.googleapis.media.MediaHttpUploader.resumableUpload(MediaHttpUploader.java:420)
    at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:336)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:524)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:448)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)

https://github.com/googlesamples/android-play-publisher-api处的示例未提供任何用于上传AAB文件的示例。对我想念的东西有任何想法吗?

1 个答案:

答案 0 :(得分:0)

HTTP请求超时时发生

java.net.SocketTimeoutException: Read timed out。在Google Http客户端api中设置的当前默认超时为20秒。

Google建议将HTTP请求https://developers.google.com/android-publisher/api-ref/edits/bundles的超时时间增加到2分钟,以上传aab文件。

以下是其操作方法:

通过创建以下方法来覆盖initialize的{​​{1}}方法:

HttpRequestInitializer

在实例化HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) { return httpRequest -> { requestInitializer.initialize(httpRequest); httpRequest.setConnectTimeout(2 * 60000); // 2 minutes connect timeout httpRequest.setReadTimeout(2 * 60000); // 2 minutes read timeout }; } 时调用setTimeout方法,如下所示:

AndroidPublisher.Builder