今回は本当に閉じ込められています。フローズンヨーグルト(2.2.0)用のAndroidアプリを開発しています。を介して利用できる特定のWebサービスに接続する必要があります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)
他の人も同様の問題に遭遇したのを見てきました。たとえば、この投稿には2つの解決策がリストされていますが、どちらもうまくいきませんでした。このスレッドはまた、私にとってはうまくいかない1つの新しい解決策を提案しています。リストされた修正のほとんどは、私が受けていた例外さえも変更しませんでした。
ご覧のとおり、私は多くのことを試みました。うまくいけば、他の誰かがそのような問題に遭遇し、それに対するさらに別の解決策を見つけました。