Search in sources :

Example 16 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project coursera-android by aporter.

the class JSONResponseHandler method handleResponse.

@Override
public List<EarthQuakeRec> handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    List<EarthQuakeRec> result = new ArrayList<EarthQuakeRec>();
    String JSONResponse = new BasicResponseHandler().handleResponse(response);
    try {
        JSONObject object = (JSONObject) new JSONTokener(JSONResponse).nextValue();
        JSONArray earthquakes = object.getJSONArray("earthquakes");
        for (int i = 0; i < earthquakes.length(); i++) {
            JSONObject tmp = (JSONObject) earthquakes.get(i);
            result.add(new EarthQuakeRec(tmp.getDouble("lat"), tmp.getDouble("lng"), tmp.getDouble("magnitude")));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return result;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 17 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project musiccabinet by hakko.

the class AbstractMusicBrainzClient method executeWSRequest.

protected String executeWSRequest(WebserviceInvocation invocation, String path, List<NameValuePair> params) throws ApplicationException {
    String response = null;
    HttpGet httpGet = new HttpGet(getURI(path, params));
    httpGet.setHeader(USER_AGENT, CLIENT_INFO);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    try {
        long elapsedMs = -currentTimeMillis();
        response = httpClient.execute(httpGet, responseHandler);
        elapsedMs += currentTimeMillis();
        sleep(Math.max(INTERVAL_MS - elapsedMs, 0));
    } catch (HttpResponseException e) {
        LOG.warn(format("MusicBrainz internal error: %d, %s", e.getStatusCode(), e.getMessage()));
        throw new ApplicationException("MusicBrainz internal error!", e);
    } catch (IOException e) {
        throw new ApplicationException("MusicBrainz communication failed!", e);
    } catch (InterruptedException e) {
        LOG.warn("MusicBrainz sleep interrupted!", e);
    }
    webserviceHistoryService.logWebserviceInvocation(invocation);
    return response;
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 18 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project libresonic by Libresonic.

the class VersionService method readLatestVersion.

/**
     * Resolves the latest available Libresonic version by screen-scraping a web page.
     *
     * @throws IOException If an I/O error occurs.
     */
private void readLatestVersion() throws IOException {
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
    HttpGet method = new HttpGet(VERSION_URL + "?v=" + getLocalVersion());
    method.setConfig(requestConfig);
    String content;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        content = client.execute(method, responseHandler);
    }
    Pattern finalPattern = Pattern.compile("LIBRESONIC_FULL_VERSION_BEGIN(.*)LIBRESONIC_FULL_VERSION_END");
    Pattern betaPattern = Pattern.compile("LIBRESONIC_BETA_VERSION_BEGIN(.*)LIBRESONIC_BETA_VERSION_END");
    try (BufferedReader reader = new BufferedReader(new StringReader(content))) {
        String line = reader.readLine();
        while (line != null) {
            Matcher finalMatcher = finalPattern.matcher(line);
            if (finalMatcher.find()) {
                latestFinalVersion = new Version(finalMatcher.group(1));
                LOG.info("Resolved latest Libresonic final version to: " + latestFinalVersion);
            }
            Matcher betaMatcher = betaPattern.matcher(line);
            if (betaMatcher.find()) {
                latestBetaVersion = new Version(betaMatcher.group(1));
                LOG.info("Resolved latest Libresonic beta version to: " + latestBetaVersion);
            }
            line = reader.readLine();
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Version(org.libresonic.player.domain.Version) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler)

Example 19 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project libresonic by Libresonic.

the class LyricsService method executeGetRequest.

private String executeGetRequest(String url) throws IOException {
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(15000).setSocketTimeout(15000).build();
    HttpGet method = new HttpGet(url);
    method.setConfig(requestConfig);
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        return client.execute(method, responseHandler);
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler)

Example 20 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project libresonic by Libresonic.

the class AudioScrobblerService method executeRequest.

private String[] executeRequest(HttpUriRequest request) throws IOException {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = client.execute(request, responseHandler);
        return response.split("\\n");
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler)

Aggregations

BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)39 HttpGet (org.apache.http.client.methods.HttpGet)26 Test (org.junit.Test)15 IOException (java.io.IOException)10 HttpPost (org.apache.http.client.methods.HttpPost)10 ArrayList (java.util.ArrayList)7 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)7 NameValuePair (org.apache.http.NameValuePair)6 HttpClient (org.apache.http.client.HttpClient)6 HttpResponseException (org.apache.http.client.HttpResponseException)6 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)5 RequestConfig (org.apache.http.client.config.RequestConfig)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2