这一次我真的被困住了。我正在为 Froyo (2.2.0) 开发 Android 应用程序。我需要连接到通过https
. 至于现在信任所有证书是我想做的事情(我将在接下来的几天内解决这个问题,但现在发布在我身上)。我已经从这篇非常有用的帖子中复制粘贴了代码。太完美了,我这样构建我的客户:
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;
}
}
但是我遇到以下错误:
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)
我看到其他人也偶然发现了类似的问题。例如,这篇文章列出了两种解决方案,但都不适合我。该线程还提出了一种新的解决方案,也不适用于我。大多数列出的修复程序甚至都没有改变我得到的异常。
如您所见,我尝试了很多事情。希望其他人遇到这样的问题并找到另一种解决方案。