Code example for URI

Methods: getHost, getPath, getPort, getScheme

0
 
        // Create client and set our specific user-agent string 
        HttpClient client = CreateClient();
        java.net.URI uri = URI.create(url);
        HttpHost host = GetHost(uri);
        HttpDelete request = new HttpDelete(uri.getPath());
        request.setHeader("User-Agent", sUserAgent);
 
        try { 
            HttpResponse response = client.execute(host, request);
 
            // Check if server response is valid 
            StatusLine status = response.getStatusLine();
            Log.i(cTag, "delete with response " + status.toString());
            return status.getStatusCode() == HttpStatus.SC_OK;
 
        } catch (IOException e) {
            throw new ApiException("Problem communicating with API", e);
        } 
    } 
 
    private HttpClient CreateClient() {
        DefaultHttpClient client = new DefaultHttpClient();
        client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(tracksUser, tracksPassword));
        return client;
    } 
 
    public synchronized String getUrlContent(String url) throws ApiException {
        if (sUserAgent == null) {
            throw new ApiException("User-Agent string must be prepared"); 
        } 
 
        // Create client and set our specific user-agent string 
        HttpClient client = CreateClient();
        java.net.URI uri = URI.create(url);
        HttpHost host = GetHost(uri);
 
 
        HttpGet request = new HttpGet(uri.getPath());
        request.setHeader("User-Agent", sUserAgent);
 
 
        try { 
            HttpResponse response = client.execute(host, request);
 
            // Check if server response is valid 
            StatusLine status = response.getStatusLine();
            Log.i(cTag, "get with response " + status.toString());
            if (status.getStatusCode() != HttpStatus.SC_OK) {
                throw new ApiException("Invalid response from server: " + 
                        status.toString());
            } 
 
            // Pull content stream from response 
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();
 
            ByteArrayOutputStream content = new ByteArrayOutputStream();
 
            // Read response into a buffered stream 
            int readBytes;
            while ((readBytes = inputStream.read(sBuffer)) != -1) {
                content.write(sBuffer, 0, readBytes);
            } 
            return new String(content.toByteArray());
 
        } catch (IOException e) {
            throw new ApiException("Problem communicating with API", e);
        } 
    } 
 
    private HttpHost GetHost(URI uri) {
        HttpHost host;
        int port = uri.getPort();
        if (port == -1) {
            port = uri.getScheme().equalsIgnoreCase("https") ? 443 : 80;
        } 
 
        host = new HttpHost(uri.getHost(), port, uri.getScheme());
        return host;
    } 
 
    protected synchronized String postContentToUrl(String url, String content) throws ApiException {
        if (sUserAgent == null) {