1212import org .apache .http .client .HttpClient ;
1313import org .apache .http .client .entity .UrlEncodedFormEntity ;
1414import org .apache .http .client .methods .HttpPost ;
15- import org .apache .http .impl .client .DefaultHttpClient ;
15+ import org .apache .http .impl .client .HttpClients ;
16+ import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
1617import org .apache .http .message .BasicNameValuePair ;
1718import org .xml .sax .SAXException ;
1819
3031 * Created by Moritz on 28.09.2015.
3132 */
3233public class TexInfo {
34+
35+ private static HttpClient client = null ;
36+
3337 private static String makeRequest (String tex , String url ) {
34- HttpClient client = new DefaultHttpClient ();
38+ if (client == null ) {
39+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager ();
40+ client = HttpClients .custom ()
41+ .setConnectionManager (cm )
42+ .build ();
43+ }
3544 //HttpPost post = new HttpPost("http://localhost/convert");
3645 HttpPost post = new HttpPost (url );
3746 try {
@@ -45,9 +54,12 @@ private static String makeRequest(String tex, String url) {
4554 while ((line = rd .readLine ()) != null ) {
4655 result += line ;
4756 }
57+ post .releaseConnection ();
4858 return result ;
4959 } catch (IOException e ) {
5060 e .printStackTrace ();
61+ } finally {
62+ post .releaseConnection ();
5163 }
5264 return "" ;
5365 }
@@ -56,6 +68,9 @@ public static Multiset<String> getIdentifiers(String tex, String url) throws XPa
5668 final Multiset <String > strings = HashMultiset .create ();
5769 //long t0 = System.nanoTime();
5870 String json = makeRequest (tex , url );
71+ if (tex .length () == 0 ) {
72+ return strings ;
73+ }
5974 //System.out.println((System.nanoTime()-t0)/1000000+"ms for "+tex);
6075 try {
6176 JSONObject jsonObject = (JSONObject ) JSONSerializer .toJSON (json );
0 commit comments