问题
我在托管在谷歌应用引擎上的 Web servlet 中使用 Apache HttpClient。我遇到的问题是,如果我在 Web 浏览器中访问 url,我会得到一个很好的JSON 结果,但是如果我尝试在 JAVA 中获取相同的 JSON 数据,我的连接会立即关闭,没有返回实体。这是我的 servlet 中的代码:
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());
}