This time I am really trapped. I am developing Android app for Froyo (2.2.0). I need to connect to certain web services available through https
. As for now trusting all certificates is the thing I want to do (I will fix that in the following days but now release is upon me). I have copy-pasted the code from this immensely helpful post. That's perfect I construct my client like that:
public class RestClient {
private static final String LOG_TAG = "RestClient";
private HttpClient httpClient;
private Context context;
public RestClient(Context context) {
this.httpClient = CustomSSLSocketFactory.getCustomHttpClient();
this.context = context;
}
public String post(String path, String requestBody,
String optFieldOfInterest) {
HttpPost httpPost = new HttpPost(getAddress() + path);
httpPost.setEntity(prepareStringEntity(requestBody));
Log.d(LOG_TAG, "Executing request:\n" + requestBody);
return executeRequest(httpPost, optFieldOfInterest);
}
private StringEntity prepareStringEntity(String requestBody) {
StringEntity entity = null;
try {
entity = new StringEntity(requestBody, "UTF-8");
entity.setContentType("application/json");
} catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG, "Error while preparing request", e);
}
return entity;
}
private String executeRequest(HttpRequestBase request)
throws ClientProtocolException, IOException {
HttpResponse httpResponse = null;
String responseString = null;
httpResponse = httpClient.execute(request); // Exception line
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String responseBody =
IOHelper.parseStream(httpResponse.getEntity().getContent());
Log.d(LOG_TAG, "Response body:\n" + responseBody);
responseString = responseBody;
} else {
Log.w(LOG_TAG, "I am not passing through here my log shows it");
}
return responseString;
}
}
However I run on the following error:
org.apache.http.NoHttpResponseException: The target server failed to respond
at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:421)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.mycompany.myproject.ws.client.MySSLHttpClient.executeRequest(MySSLHttpClient.java:70)
at com.mycompany.myproject.ws.client.MySSLHttpClient.post(MySSLHttpClient.java:120)
at com.mycompany.myproject.ws.asynctasks.RegisterDeviceAsyncTask.doInBackground(RegisterDeviceAsyncTask.java:57)
at com.mycompany.myproject.ws.asynctasks.RegisterDeviceAsyncTask.doInBackground(RegisterDeviceAsyncTask.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
I have seen that other people also stumbled upon similar problems. For example this post is listing two solutions, but neither worked for me. This thread also proposes one new solution, also not working for me. Most of the listed fixes didn't even change the exception I was getting.
As you can see I have tried many things. Hopefully somebody else run into such an issue and found yet another solution for it.