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 RequestBad 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.