Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.annotation.TargetApi;
import android.net.Network;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import static android.os.Build.VERSION_CODES.LOLLIPOP;

public class Main {
    /**
     * Given a string url, connects and returns response code
     *
     * @param urlString       string to fetch
     * @param network       network
     * @param readTimeOutMs       read time out
     * @param connectionTimeOutMs       connection time out
     * @param urlRedirect       should use urlRedirect
     * @param useCaches       should use cache
     * @return httpResponseCode http response code
     * @throws IOException
     */

    @TargetApi(LOLLIPOP)
    public static int checkUrlWithOptionsOverNetwork(String urlString, Network network, int readTimeOutMs,
            int connectionTimeOutMs, boolean urlRedirect, boolean useCaches) throws IOException {
        if (network == null) {
            return -1;
        }
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) network.openConnection(url);
        connection.setReadTimeout(readTimeOutMs /* milliseconds */);
        connection.setConnectTimeout(connectionTimeOutMs /* milliseconds */);
        connection.setRequestMethod("GET");
        connection.setInstanceFollowRedirects(urlRedirect);
        connection.setUseCaches(useCaches);
        // Starts the query
        connection.connect();
        int responseCode = connection.getResponseCode();
        connection.disconnect();
        return responseCode;
    }
}