問題
Google アプリ エンジンでホストされている Web サーブレットで Apache HttpClient を使用しています。私が抱えている問題は、Web ブラウザーで URL にアクセスすると、良いJSON 結果が得られることですが、JAVA でこれと同じ JSON データを取得しようとすると、エンティティが返されずに接続がすぐに閉じられます。私のサーブレットのコードは次のとおりです。
try {
// Pooling code from: http://stackoverflow.com/a/13763608
PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager(
SchemeRegistryFactory.createDefault());
cxMgr.setMaxTotal(100);
cxMgr.setDefaultMaxPerRoute(20);
HttpClient client = new DefaultHttpClient(cxMgr);
URIBuilder uriQuery = new URIBuilder();
uriQuery.setScheme("https");
uriQuery.setHost("doctorbase.com");
uriQuery.setPath("/api/dr/search_rest");
uriQuery.setParameter("location", user.getLocation().getName());
URI uri = uriQuery.build();
System.out.println("Setting up get: " + uri.toString());
HttpGet getRequest = new HttpGet(uri);
System.out.println("Calling execute");
HttpResponse docApiResponse = client.execute(getRequest);
System.out.println("Getting Entity");
HttpEntity entity = docApiResponse.getEntity();
} catch (Exception e) {
System.out.println("Exception getting doctors: "
+ e.toString());
e.printStackTrace();
}
Google App Engine で表示されるログは次のとおりです (client.Execute(getRequest) 行がメソッドから飛び出しているように見えますが、それは try-catch ブロック内にあり、catch が実行されていないため、発生すべきではありません。
2013-03-04 19:44:54.593 [s~mybloodpressurelog/1.365731215144292261].<stdout>: Calling getNearbyDoctorInfo
I 2013-03-04 19:44:54.747 [s~mybloodpressurelog/1.365731215144292261].<stdout>: Setting up get: https://doctorbase.com/api/dr/search_rest?location=Sacramento%2C+California
I 2013-03-04 19:44:54.747 [s~mybloodpressurelog/1.365731215144292261].<stdout>: Calling execute
D 2013-03-04 19:44:54.752 org.apache.http.impl.conn.PoolingClientConnectionManager requestConnection: Connection request: [route: {s}->https://doctorbase.com][total kept alive:
D 2013-03-04 19:44:54.753 org.apache.http.impl.conn.PoolingClientConnectionManager leaseConnection: Connection leased: [id: 1][route: {s}->https://doctorbase.com][total kept al
D 2013-03-04 19:44:54.758 org.apache.http.impl.conn.DefaultClientConnection shutdown: Connection org.apache.http.impl.conn.DefaultClientConnection@f0850b shut down
D 2013-03-04 19:44:54.758 org.apache.http.impl.conn.DefaultClientConnection close: Connection org.apache.http.impl.conn.DefaultClientConnection@f0850b closed
D 2013-03-04 19:44:54.758 org.apache.http.impl.conn.PoolingClientConnectionManager releaseConnection: Connection released: [id: 1][route: {s}->https://doctorbase.com][total kep
謎を解く助けは、すぐに接続を閉じることです。
作業コード
(私を正しい方向に向けてくれた Fahad に感謝します!):
URL doctorUrl = new URL(
"https://doctorbase.com/api/dr/search_rest?"
+ user.getLocation().getName()
.replaceAll(" ", "%2C+"));
try {
System.out.println("IS to String");
BufferedReader jsonReader = new BufferedReader(
new InputStreamReader(doctorUrl.openStream()));
String json = jsonReader.readLine();
System.out.println("Doctor JSON: " + json);
DoctorApiResult drs = new Gson().fromJson(json,
DoctorApiResult.class);
System.out.println("Deserialized Doctors ");
int numberOfDoctors = drs.getDrs().size();
System.out.println("Number of doctors found: "
+ numberOfDoctors);
DoctorBean[] doctors = new DoctorBean[numberOfDoctors];
drs.getDrs().toArray(doctors);
request.setAttribute("drs", doctors);
} catch (Exception e) {
System.out
.println("Exception getting doctors: " + e.toString());
e.printStackTrace();
request.setAttribute("error", e.toString());
}