2

I'm using the Google Http Java Client library in my Android project. I have the following basic code for a POST request to the server on an Android app:

    final GenericUrl url = new GenericUrl(postURL);
    final HttpContent content = new JsonHttpContent(JSON_FACTORY, jsondata);
    // requestFactory initiated statically as class object
    HttpRequest request = requestFactory.buildPostRequest(url, content); 
    HttpResponse response = request.execute();

The 'RequestFactory' is defined as static in the class that holds the POST method.

   static final HttpRequestFactory requestFactory = HTTP_TRANSPORT
        .createRequestFactory(new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest request) {
                request.setParser(new JsonCParser(JSON_FACTORY));
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType("application/json");
                request.setHeaders(headers);
            }
        });

The same requestfactory is used for GET requests that haven't cause any problems. Only the POST method causes a 400. I checked the server and nothing is being sent to it. The message in the logs is the following: Bad Request

Bad Request - Invalid Header


HTTP Error 400. The request has an invalid header name.

com.google.api.client.http.HttpResponseException: 400 Bad Request Bad Request

Bad Request - Invalid Header


HTTP Error 400. The request has an invalid header name.

at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1095)

I tried troubleshooting. The connection object in the NetHttpRequest class of the library has an HttpEngineFailure object with an EOFException which makes me think that connection is not being closed and this is a best practice issue rather than a bug.

On the other hand, I tried it on an Android API 2.2 emulator and the POST request work fine there. Indicating that it's an HttpUrlConnection issue.

I asked this question on the product group page but didn't get a response so I though maybe some Java/Android experts here could help me figure this one out.

2 Answers 2

1

Instead of creating JsonHttpContent you can do something like this :

HttpRequest request = getHttpRequestFactory()//
        .buildPostRequest(new GenericUrl(apiUrl), ByteArrayContent.fromString("application/json", mJsonObject.toString()));
    request.setParser(new JacksonFactory().createJsonObjectParser());
Sign up to request clarification or add additional context in comments.

Comments

0

Different versions of Android uses different versions of HttpConnection and thus behave differently. It all depends on what HttpTransport you are using.

From the docs :

If you are building an application targeted at Gingerbread or higher, you should use NetHttpTransport. This is based on HttpURLConnection that is built into the Android SDK and is found in all Java SDKs. However, in prior Android SDKs the implementation of HttpURLConnection was buggy, and the Apache HTTP Client was preferred. So for those SDKs, please use ApacheHttpTransport. If you are building an Android application that needs to work with all Android SDKs, simply call AndroidHttp.newCompatibleTransport() and it will decide of these two to use based on the Android SDK level.

3 Comments

I use the AndroidHttp.newCompatibleTransport() and as I mentioned the POST goes through fine in an Android 2.2 emulator which means that transport is being selected correctly.
ah ok , so the real device is also Android 2.2 and failing ?
The real device is 4.2 and failing. So it's HttpUrlConnection that's causing the problem and not Apache HttpClient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.