From 06789a80a15a3097ed51db14f5a666589c8cce01 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Thu, 18 May 2017 20:16:56 -0700 Subject: [PATCH] Switched to OkHttp. --- build.gradle | 8 ++++---- kobalt/src/Build.kt | 8 ++++---- .../erik/mobibot/modules/StockQuote.java | 17 ++++++++--------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/build.gradle b/build.gradle index 20ce201..502bc00 100644 --- a/build.gradle +++ b/build.gradle @@ -53,18 +53,18 @@ dependencies { compile 'pircbot:pircbot:1.5.0' - compile 'commons-codec:commons-codec:1.10' + //compile 'commons-codec:commons-codec:1.10' compile 'commons-logging:commons-logging:1.2' compile 'commons-net:commons-net:3.6' compile 'commons-cli:commons-cli:1.4' - compile 'commons-httpclient:commons-httpclient:3.1' + compile 'com.squareup.okhttp3:okhttp:3.8.0' compile 'oro:oro:2.0.8' compile 'org.jsoup:jsoup:1.10.2' - compile 'com.rometools:rome:1.7.1' + compile 'com.rometools:rome:1.7.3' compile 'org.slf4j:slf4j-log4j12:1.7.25' - compile 'org.json:json:20160810' + compile 'org.json:json:20170516' compile 'org.ostermiller:utils:1.07.00' compile 'net.objecthunter:exp4j:0.4.8' diff --git a/kobalt/src/Build.kt b/kobalt/src/Build.kt index 3362210..4eed577 100644 --- a/kobalt/src/Build.kt +++ b/kobalt/src/Build.kt @@ -56,18 +56,18 @@ val p = project { compile("pircbot:pircbot:1.5.0") - compile("commons-codec:commons-codec:1.10") + //compile("commons-codec:commons-codec:1.10") compile("commons-logging:commons-logging:1.2") compile("commons-net:commons-net:3.6") compile("commons-cli:commons-cli:1.4") - compile("commons-httpclient:commons-httpclient:3.1") + compile("com.squareup.okhttp3:okhttp:3.8.0") compile("oro:oro:2.0.8") compile("org.jsoup:jsoup:1.10.2") - compile("com.rometools:rome:1.7.1") + compile("com.rometools:rome:1.7.3") compile("org.slf4j:slf4j-log4j12:1.7.25") - compile("org.json:json:20160810") + compile("org.json:json:20170516") compile("org.ostermiller:utils:1.07.00") compile("net.objecthunter:exp4j:0.4.8") diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java index 7545852..6561504 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java @@ -33,8 +33,9 @@ package net.thauvin.erik.mobibot.modules; import com.Ostermiller.util.CSVParser; import net.thauvin.erik.mobibot.Mobibot; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.methods.GetMethod; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; import java.io.IOException; @@ -87,14 +88,12 @@ final public class StockQuote extends AbstractModule { */ private void run(final Mobibot bot, final String sender, final String symbol) { try { - final HttpClient client = new HttpClient(); - client.getHttpConnectionManager().getParams().setConnectionTimeout(Mobibot.CONNECT_TIMEOUT); - client.getHttpConnectionManager().getParams().setSoTimeout(Mobibot.CONNECT_TIMEOUT); - final GetMethod getMethod = new GetMethod(YAHOO_URL + symbol.toUpperCase()); - client.executeMethod(getMethod); + final OkHttpClient client = new OkHttpClient(); + final Request request = new Request.Builder().url(YAHOO_URL + symbol.toUpperCase()).build(); + final Response response = client.newCall(request).execute(); - final String[][] lines = CSVParser.parse(getMethod.getResponseBodyAsString()); + final String[][] lines = CSVParser.parse(response.body().string()); if (lines.length > 0) { final String[] quote = lines[0]; @@ -133,7 +132,7 @@ final public class StockQuote extends AbstractModule { } else { bot.send(sender, "No data returned."); } - } catch (IOException e) { + } catch (NullPointerException | IOException e) { bot.getLogger().debug("Unable to retrieve stock quote for: " + symbol, e); bot.send(sender, "An error has occurred retrieving a stock quote: " + e.getMessage()); }