Search code examples
androidpostandroid-networking

Android http post request fails when targeting new API version


I've had no problems with my HTTP POST request until now.

try {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 1000);
        client.setParams(params);
        HttpPost post = new HttpPost(NETWORK_CHECK_CONTROLLER_ADDRESS);
        ResponseHandler<String> handler = new BasicResponseHandler();
        HttpResponse response = client.execute(post);// here in 4.0
        String responseString = handler.handleResponse(response);
        JSONObject json = new JSONObject(responseString);
    } catch (Exception e) {

        logReporter.add("Could not connect to controller");
        Log.d("4.0 tests",e.getCause()+" ");
        return false;
    }

    return true;

This request never failed or threw an exception when I was targeting API version 8. as specified in my manifest.

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

Before it was

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

Now my post request fails everytime and the exception message is always Null. What is different between android api 8 and 15 in terms of post requests?

Yes, I'm sure the server I'm POST'ing to is online. When I switch it back to api 8 all is fine.


Solution

  • Can it be you're posting from the UI thread? In this case an exception will be thrown starting from Gingerbread.