Android Open Source - Helpers Network Helper






From Project

Back to project page Helpers.

License

The source code is released under:

MIT License

If you think the Android project Helpers listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package at.wrdlbrnft.helpers;
//  w  w w .ja  v  a 2s . c o  m
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.io.InputStream;

import at.wrdlbrnft.helpers.credentials.Credentials;
import at.wrdlbrnft.helpers.credentials.CredentialsFactory;

public class NetworkHelper {

    private static final String CREDENTIALS_ENCODING = "UTF-8";

  public static boolean hasConnection(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiNetwork = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileNetwork = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    NetworkInfo activeNetwork = manager.getActiveNetworkInfo();

    return isNetworkConnected(wifiNetwork) || isNetworkConnected(mobileNetwork) || isNetworkConnected(activeNetwork);
  }

  private static boolean isNetworkConnected(NetworkInfo info) {
    return info != null && info.isConnected();
  }

    public static String read(String url) throws IOException {
        return read(url, null);
    }

    public static String read(String url, String username, String password) throws IOException {
        Credentials credentials = CredentialsFactory.create(username, password);
        return read(url, credentials);
    }

    public static String read(String url, Credentials credentials) throws IOException {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);

        if (credentials != null) {
            request.addHeader(BasicScheme.authenticate(credentials.toUserPasswordCredentials(), CREDENTIALS_ENCODING, false));
        }

        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();

        return StreamHelper.read(stream);
    }

    public static InputStream open(String url) throws IOException {
        return open(url, null);
    }

    public static InputStream open(String url, String username, String password) throws IOException {
        Credentials credentials = CredentialsFactory.create(username, password);
        return open(url, credentials);
    }

    public static InputStream open(String url, Credentials credentials) throws IOException {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);

        if (credentials != null) {
            client.getCredentialsProvider().setCredentials(
                    new AuthScope(null, -1),
                    credentials.toUserPasswordCredentials());
        }

        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        return entity.getContent();
    }
}




Java Source Code List

at.wrdlbrnft.helpers.ApplicationTest.java
at.wrdlbrnft.helpers.AssetHelper.java
at.wrdlbrnft.helpers.Base64Coder.java
at.wrdlbrnft.helpers.BitHelper.java
at.wrdlbrnft.helpers.BitmapHelper.java
at.wrdlbrnft.helpers.Bundles.java
at.wrdlbrnft.helpers.CanvasHelper.java
at.wrdlbrnft.helpers.Container.java
at.wrdlbrnft.helpers.Containers.java
at.wrdlbrnft.helpers.Dates.java
at.wrdlbrnft.helpers.ExtrasBuilder.java
at.wrdlbrnft.helpers.IntentHelper.java
at.wrdlbrnft.helpers.LayoutHelper.java
at.wrdlbrnft.helpers.LogHelper.java
at.wrdlbrnft.helpers.Maps.java
at.wrdlbrnft.helpers.NetworkHelper.java
at.wrdlbrnft.helpers.Numbers.java
at.wrdlbrnft.helpers.PackageHelper.java
at.wrdlbrnft.helpers.ReflectionHelper.java
at.wrdlbrnft.helpers.SerializationHelper.java
at.wrdlbrnft.helpers.Sets.java
at.wrdlbrnft.helpers.StreamHelper.java
at.wrdlbrnft.helpers.Strings.java
at.wrdlbrnft.helpers.TypeHelper.java
at.wrdlbrnft.helpers.ViewHelper.java
at.wrdlbrnft.helpers.credentials.CredentialsFactory.java
at.wrdlbrnft.helpers.credentials.Credentials.java
at.wrdlbrnft.helpers.lists.Difference.java
at.wrdlbrnft.helpers.lists.Lists.java
at.wrdlbrnft.helpers.lists.UniqueIdProvider.java
at.wrdlbrnft.helpers.lists.UpdateInfo.java
at.wrdlbrnft.helpers.lists.UpdateValueContainer.java
at.wrdlbrnft.helpers.time.Time.java
at.wrdlbrnft.helpers.time.Times.java