Code example for HttpURLConnection

Methods: setRequestMethodaddRequestPropertysetConnectTimeoutsetDoInputsetReadTimeout

0
    private HttpURLConnection prepareGet(String apiPath, boolean withToken)
            throws IOException { 
        URL url = new URL(account.server + apiPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        prepareSSL(conn, true);
        conn.setReadTimeout(30000);
        conn.setConnectTimeout(15000);
     
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        if (withToken)
            conn.addRequestProperty("Authorization", "Token " + account.token);
         
        //Log.d(DEBUG_TAG, "get from " + url.getPath()); 
        return conn;
    } 
     
    private HttpURLConnection prepareGet(String apiPath) throws IOException {
        return prepareGet(apiPath, true);
    } 
Stop searching for code, let great code find you!  Add Codota to your java IDE