Example usage for java.net HttpURLConnection getResponseCode

List of usage examples for java.net HttpURLConnection getResponseCode

Introduction

In this page you can find the example usage for java.net HttpURLConnection getResponseCode.

Prototype

public int getResponseCode() throws IOException 

Source Link

Document

Gets the status code from an HTTP response message.

Usage

From source file:com.bellman.bible.service.common.CommonUtils.java

/** return true if URL is accessible
 * //from ww  w.jav  a  2 s.com
 * Since Android 3 must do on different or NetworkOnMainThreadException is thrown
 */
public static boolean isHttpUrlAvailable(final String urlString) {
    boolean isAvailable = false;
    final int TIMEOUT_MILLIS = 3000;

    try {
        class CheckUrlThread extends Thread {
            public boolean checkUrlSuccess = false;

            public void run() {
                HttpURLConnection connection = null;
                try {
                    // might as well test for the url we need to access
                    URL url = new URL(urlString);

                    Log.d(TAG, "Opening test connection");
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(TIMEOUT_MILLIS);
                    connection.setReadTimeout(TIMEOUT_MILLIS);
                    connection.setRequestMethod("HEAD");
                    Log.d(TAG, "Connecting to test internet connection");
                    connection.connect();
                    checkUrlSuccess = (connection.getResponseCode() == HttpURLConnection.HTTP_OK);
                    Log.d(TAG, "Url test result for:" + urlString + " is " + checkUrlSuccess);
                } catch (IOException e) {
                    Log.i(TAG, "No internet connection");
                    checkUrlSuccess = false;
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
        }

        CheckUrlThread checkThread = new CheckUrlThread();
        checkThread.start();
        checkThread.join(TIMEOUT_MILLIS);
        isAvailable = checkThread.checkUrlSuccess;
    } catch (InterruptedException e) {
        Log.e(TAG, "Interrupted waiting for url check to complete", e);
    }
    return isAvailable;
}

From source file:com.example.scanqr.AbstractGetNameTask.java

/**
 * Contacts the user info server to get the profile of the user and extracts
 * the first name of the user from the profile. In order to authenticate
 * with the user info server the method first fetches an access token from
 * Google Play services./*ww w  .ja va  2 s . co m*/
 * @return 
 * @return 
 * 
 * @throws IOException
 *             if communication with user info server failed.
 * @throws JSONException
 *             if the response from the server could not be parsed.
 */
private void fetchNameFromProfileServer() throws IOException, JSONException {
    String token = fetchToken();
    URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    int sc = con.getResponseCode();
    if (sc == 200) {
        InputStream is = con.getInputStream();
        GOOGLE_USER_DATA = readResponse(is);
        is.close();

        Intent intent = new Intent(mActivity, RedirectClass.class);
        intent.putExtra("email_id", mEmail);
        mActivity.startActivity(intent);
        mActivity.finish();
        return;
    } else if (sc == 401) {
        GoogleAuthUtil.invalidateToken(mActivity, token);
        onError("Server auth error, please try again.", null);
        //Toast.makeText(mActivity, "Please try again", Toast.LENGTH_SHORT).show();
        //mActivity.finish();
        return;
    } else {
        onError("Server returned the following error code: " + sc, null);
        return;
    }
}

From source file:com.google.android.gms.auth.sample.helloauth.AbstractGetNameTask.java

/**
 * Contacts the user info server to get the profile of the user and extracts the first name
 * of the user from the profile. In order to authenticate with the user info server the method
 * first fetches an access token from Google Play services.
 * @throws IOException if communication with user info server failed.
 * @throws JSONException if the response from the server could not be parsed.
 *///from  w w w . j a  v a 2  s  . c  o m
private void fetchNameFromProfileServer() throws IOException, JSONException {
    String token = fetchToken();
    if (token == null) {
        // error has already been handled in fetchToken()
        return;
    }
    URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    int sc = con.getResponseCode();
    if (sc == 200) {
        InputStream is = con.getInputStream();
        String name = getFirstName(readResponse(is));
        mActivity.show("Hello " + name + "!");
        is.close();
        return;
    } else if (sc == 401) {
        GoogleAuthUtil.invalidateToken(mActivity, token);
        onError("Server auth error, please try again.", null);
        Log.i(TAG, "Server auth error: " + readResponse(con.getErrorStream()));
        return;
    } else {
        onError("Server returned the following error code: " + sc, null);
        return;
    }
}

From source file:com.manish.googleprofiledemo.AbstractGetNameTask.java

/**
 * Contacts the user info server to get the profile of the user and extracts
 * the first name of the user from the profile. In order to authenticate
 * with the user info server the method first fetches an access token from
 * Google Play services.//from   w  w w .  jav a2 s  .c o  m
 * @return 
 * @return 
 * 
 * @throws IOException
 *             if communication with user info server failed.
 * @throws JSONException
 *             if the response from the server could not be parsed.
 */
private void fetchNameFromProfileServer() throws IOException, JSONException {
    String token = fetchToken();
    URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    int sc = con.getResponseCode();
    if (sc == 200) {
        InputStream is = con.getInputStream();
        GOOGLE_USER_DATA = readResponse(is);
        is.close();

        Intent intent = new Intent(mActivity, HomeActivity.class);
        intent.putExtra("email_id", mEmail);
        mActivity.startActivity(intent);
        mActivity.finish();
        return;
    } else if (sc == 401) {
        GoogleAuthUtil.invalidateToken(mActivity, token);
        onError("Server auth error, please try again.", null);
        //Toast.makeText(mActivity, "Please try again", Toast.LENGTH_SHORT).show();
        //mActivity.finish();
        return;
    } else {
        onError("Server returned the following error code: " + sc, null);
        return;
    }
}

From source file:com.nnp.webike.AbstractGetNameTask.java

/**
 * Contacts the user info server to get the profile of the user and extracts
 * the first name of the user from the profile. In order to authenticate
 * with the user info server the method first fetches an access token from
 * Google Play services./*from  w  w  w .  j a va 2  s.co m*/
 * @return 
 * @return 
 * 
 * @throws IOException
 *             if communication with user info server failed.
 * @throws JSONException
 *             if the response from the server could not be parsed.
 */
private void fetchNameFromProfileServer() throws IOException, JSONException {
    String token = fetchToken();
    URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    int sc = con.getResponseCode();
    if (sc == 200) {
        InputStream is = con.getInputStream();
        GOOGLE_USER_DATA = readResponse(is);
        is.close();

        Intent intent = new Intent(mActivity, MainActivity.class);
        intent.putExtra("email_id", mEmail);
        mActivity.startActivity(intent);
        mActivity.finish();
        return;
    } else if (sc == 401) {
        GoogleAuthUtil.invalidateToken(mActivity, token);
        onError("Server auth error, please try again.", null);
        //Toast.makeText(mActivity, "Please try again", Toast.LENGTH_SHORT).show();
        //mActivity.finish();
        return;
    } else {
        onError("Server returned the following error code: " + sc, null);
        return;
    }
}

From source file:com.cloudbees.mtslaves.client.RemoteReference.java

protected void drain(HttpURLConnection con) throws IOException {
    if (con.getResponseCode() >= 500) {
        String error = IOUtils.toString(con.getErrorStream());
        con.getErrorStream().close();/*  w w  w.j a v a 2 s . c o  m*/
        throw new IOException("Error received:" + con.getResponseCode() + "\n" + error);
    } else {
        IOUtils.copy(con.getInputStream(), new NullOutputStream());
        con.getInputStream().close();
    }
}

From source file:com.hdezninirola.fantasywithdrawer.utils.AbstractGetNameTask.java

/**
 * Contacts the user info server to get the profile of the user and extracts the first name
 * of the user from the profile. In order to authenticate with the user info server the method
 * first fetches an access token from Google Play services.
 * @throws java.io.IOException if communication with user info server failed.
 * @throws org.json.JSONException if the response from the server could not be parsed.
 *//*from   w  w w . j ava 2s  .  c  o m*/
private void fetchNameFromProfileServer() throws IOException, JSONException {
    String token = fetchToken();
    if (token == null) {
        // error has already been handled in fetchToken()
        return;
    }
    URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    int sc = con.getResponseCode();
    if (sc == 200) {
        InputStream is = con.getInputStream();
        String name = readResponse(is);
        mActivity.show(name);
        is.close();
        return;
    } else if (sc == 401) {
        GoogleAuthUtil.invalidateToken(mActivity, token);
        onError("Server auth error, please try again.", null);
        Log.i(TAG, "Server auth error: " + readResponse(con.getErrorStream()));
        return;
    } else {
        onError("Server returned the following error code: " + sc, null);
        return;
    }
}

From source file:com.hinj.app.google.plus.AbstractGetNameTask.java

/**
 * Contacts the user info server to get the profile of the user and extracts
 * the first name of the user from the profile. In order to authenticate
 * with the user info server the method first fetches an access token from
 * Google Play services./*from   w  ww .  j  a  v  a  2s. co  m*/
 * @return 
 * @return 
 * 
 * @throws IOException
 *             if communication with user info server failed.
 * @throws JSONException
 *             if the response from the server could not be parsed.
 */
private void fetchNameFromProfileServer() throws IOException, JSONException {
    String token = fetchToken();
    URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    int sc = con.getResponseCode();
    if (sc == 200) {
        InputStream is = con.getInputStream();
        GOOGLE_USER_DATA = readResponse(is);
        is.close();

        Intent intent = new Intent(mActivity, DashBoardActivity.class);
        intent.putExtra("email_id", mEmail);
        mActivity.startActivity(intent);
        mActivity.finish();
        return;
    } else if (sc == 401) {
        GoogleAuthUtil.invalidateToken(mActivity, token);
        onError("Server auth error, please try again.", null);
        //Toast.makeText(mActivity, "Please try again", Toast.LENGTH_SHORT).show();
        //mActivity.finish();
        return;
    } else {
        onError("Server returned the following error code: " + sc, null);
        return;
    }
}

From source file:com.mb.kids_mind.AbstractGetNameTask.java

/**
 * Contacts the user info server to get the profile of the user and extracts the first name
 * of the user from the profile. In order to authenticate with the user info server the method
 * first fetches an access token from Google Play services.
 * @throws IOException if communication with user info server failed.
 * @throws JSONException if the response from the server could not be parsed.
 *//*  w  w  w. ja  va2s .c  om*/
private void fetchNameFromProfileServer() throws IOException, JSONException {
    String token = fetchToken();
    Log.v(TAG, "token" + token);
    if (token == null) {
        // error has already been handled in fetchToken()
        return;
    }
    URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + token);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    int sc = con.getResponseCode();
    if (sc == 200) {
        InputStream is = con.getInputStream();
        String name = getFirstName(readResponse(is));
        // mActivity.show("Hello " + name + "!");
        is.close();
        return;
    } else if (sc == 401) {
        GoogleAuthUtil.invalidateToken(mActivity, token);
        onError("Server auth error, please try again.", null);
        Log.i(TAG, "Server auth error: " + readResponse(con.getErrorStream()));
        return;
    } else {
        onError("Server returned the following error code: " + sc, null);
        return;
    }
}