DefaultHttpClient#execute 不具合
ええと、以下な実装なんですが
InputStream is = httpclient.execute(method, new ResponseHandler<InputStream>() { @Override public InputStream handleResponse(HttpResponse response) throws ClientProtocolException, IOException { switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: return response.getEntity().getContent(); default: throw new RuntimeException("HTTP Status is " + response.getStatusLine().getStatusCode()); } } });
戻った InputStream から BufferedReader を生成して読み込むあたりで IOException が発生している模様。どうも response.getEntity().getContent() の戻りがアレらしい。
で、以下な部分を
case HttpStatus.SC_OK: return response.getEntity().getContent();
こうしてみるとレスポンスが取得できているご様子。
case HttpStatus.SC_OK: return EntityUtils.toString(response.getEntity(), "UTF-8");
あらららこれ何スか?
つうか以前の実装は何故に動いているのかがスデに謎。JSONFunction もこれ方式にして修正してみます。
無事
動作を確認。つうかこの実装は某所に残ったままなんだけど大丈夫なのかな。
つーことで
JSONFunction は以下なカンジになってます。試験未実施。
package jp.shuri.yamanetoshi.json; import java.io.IOException; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class JSONFunctions { private static String getHTTPResponseBodyString(HttpRequestBase request, DefaultHttpClient httpclient) throws IOException, ClientProtocolException, RuntimeException { return httpclient.execute(request, new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: return EntityUtils.toString(response.getEntity(), "UTF-8"); default: throw new RuntimeException("HTTP Status is " + response.getStatusLine().getStatusCode()); } } }); } public static String GETfromURL(String url, DefaultHttpClient httpclient) throws IOException, ClientProtocolException, RuntimeException { HttpGet request = new HttpGet(url); return getHTTPResponseBodyString(request, httpclient); } public static String POSTfromURL(String url, DefaultHttpClient httpclient, List<NameValuePair> params) throws IOException, ClientProtocolException, RuntimeException { HttpPost request = new HttpPost(url); request.setEntity(new UrlEncodedFormEntity(params)); return getHTTPResponseBodyString(request, httpclient); } public static String PUTfromURL(String url, DefaultHttpClient httpclient, List<NameValuePair> params) throws IOException, ClientProtocolException, RuntimeException { HttpPut request = new HttpPut(url); request.setEntity(new UrlEncodedFormEntity(params)); return getHTTPResponseBodyString(request, httpclient); } public static String DELETEfromURL(String url, DefaultHttpClient httpclient) throws IOException, ClientProtocolException, RuntimeException { return httpclient.execute(new HttpDelete(url), new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: return null; default: throw new RuntimeException("HTTP Status is " + response.getStatusLine().getStatusCode()); } } }); } }