问题
I have a Google Cloud Endpoints API method as follows.
@ApiMethod(name = "setImage", httpMethod = ApiMethod.HttpMethod.POST)
public void setImage(HttpServletRequest httpRequest,
com.google.appengine.api.users.User appEngineUser,
@Named("image") String image)
throws IOException, OAuthRequestException, ForbiddenException,
com.google.api.server.spi.response.NotFoundException {
// Write the image text to a pojo and save the pojo in the datastore.
}
I generate client libraries for iOS and Android from this API. In both mobile apps, I use the camera to take a photo, resample the bitmap from the camera down if necessary, compress it to JPEG and encode it as a string using the data URI scheme.
On iOS, this all works just fine. I end up uploading about 436K characters. On Android, however, I get a "413 (Request Entity Too Large)" HTTP response from the server. This happens even if I resample the image right down to the point where I'm only sending about 39K for the JPEG.
Why? The stack trace for Android is this.
com.google.api.client.googleapis.json.GoogleJsonResponseException: 413 Request Entity Too Large
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
08-24 15:39:26.046 26980-26980/cc.biketracker.android W/System.err﹕ at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
08-24 15:39:26.046 26980-26980/cc.biketracker.android W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
08-24 15:39:26.046 26980-26980/cc.biketracker.android W/System.err﹕ at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at cc.biketracker.android.task.SetDeviceBikeImageTask.doInBackground(SetDeviceBikeImageTask.java:45)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at cc.biketracker.android.task.SetDeviceBikeImageTask.doInBackground(SetDeviceBikeImageTask.java:16)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-24 15:39:26.047 26980-26980/cc.biketracker.android W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
回答1:
I believe it's because you're using image as a URL parameter rather than as the resource. I'd suggest changing @Named("image") String image
to be SetImageRequest request
and make a SetImageRequest
class which contain the encoded String. The reason it probably works on iOS and not Android is because the iOS client uses JSON-RPC, which doesn't use URL parameters at all.
来源:https://stackoverflow.com/questions/32185522/error-413-request-entity-too-large-on-a-google-cloud-endpoints-api-call-from-a