Example usage for android.util Base64 encodeToString

List of usage examples for android.util Base64 encodeToString

Introduction

In this page you can find the example usage for android.util Base64 encodeToString.

Prototype

public static String encodeToString(byte[] input, int flags) 

Source Link

Document

Base64-encode the given data and return a newly allocated String with the result.

Usage

From source file:com.example.bookstoremb.utils.RestClient.java

/**
 * create authorization for header of request
 * /* www . j a v  a2  s. co m*/
 * @return
 */
private String getAuthorization() {
    return "Basic "
            + Base64.encodeToString((Constants.USERNAME + ":" + Constants.PASSWORD).getBytes(), Base64.NO_WRAP);
}

From source file:org.openbmap.soapclient.CheckServerTask.java

/**
 * Sends a https request to website to check if server accepts user name and password
 * @return true if server confirms credentials
 *///from w  ww  . jav  a 2  s .co m
private boolean credentialsAccepted(String user, String password) {

    if (user == null || password == null) {
        return false;
    }

    final DefaultHttpClient httpclient = new DefaultHttpClient();
    final HttpPost httppost = new HttpPost(Preferences.PASSWORD_VALIDATION_URL);
    try {
        final String authorizationString = "Basic "
                + Base64.encodeToString((user + ":" + password).getBytes(), Base64.NO_WRAP);
        httppost.setHeader("Authorization", authorizationString);
        final HttpResponse response = httpclient.execute(httppost);

        final int reply = response.getStatusLine().getStatusCode();
        if (reply == 200) {
            Log.v(TAG, "Server accepted credentials");
            return true;
        } else if (reply == 401) {
            Log.e(TAG, "Server authentication failed");
            return false;
        } else {
            Log.w(TAG, "Generic error: server reply " + reply);
            return false;
        }
        // TODO: redirects (301, 302) are NOT handled here
        // thus if something changes on the server side we're dead here
    } catch (final ClientProtocolException e) {
        Log.e(TAG, e.getMessage(), e);
    } catch (final IOException e) {
        Log.e(TAG, "I/O exception while checking credentials " + e.getMessage(), e);
    }
    return false;
}

From source file:com.ferid.app.frequentcontacts.selectnumber.SelectNumberActivity.java

/**
 * Retrieve photo of the given contact with contact ID
 * @param contactId//from  www . j  a  va 2  s  .c om
 * @return
 */
private String retrievePhoto(int contactId) {
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

    Cursor cursor = getContentResolver().query(photoUri, new String[] { ContactsContract.Contacts.Photo.PHOTO },
            null, null, null);

    if (cursor == null) {
        return "";
    }

    try {
        if (cursor.moveToFirst()) {
            byte[] data = cursor.getBlob(0);
            if (data != null) {
                //byte[] to String convertion
                return Base64.encodeToString(data, Base64.DEFAULT);
            }
        }
    } finally {
        cursor.close();
    }

    return "";
}

From source file:com.bourke.kitchentimer.utils.Utils.java

public static void notifyServer(final String serverUrl) {
    new Thread(new Runnable() {
        @Override/*  w ww. ja  v  a 2s.  co m*/
        public void run() {
            try {
                Log.d("Utils", "notifyServer:" + serverUrl);
                DefaultHttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(serverUrl);

                URL url = new URL(serverUrl);
                String userInfo = url.getUserInfo();
                if (userInfo != null) {
                    httpget.addHeader("Authorization",
                            "Basic " + Base64.encodeToString(userInfo.getBytes(), Base64.NO_WRAP));
                }

                HttpResponse response = httpclient.execute(httpget);
                response.getEntity().getContent().close();
                httpclient.getConnectionManager().shutdown();

                int status = response.getStatusLine().getStatusCode();
                if (status < 200 || status > 299) {
                    throw new Exception(response.getStatusLine().toString());
                }
            } catch (Exception ex) {
                Log.e("Utils", "Error notifying server: ", ex);
            }
        }
    }).start();
}

From source file:gr.ndre.scuttloid.APITask.java

protected void addAuthHeader(HttpRequestBase request) {
    String authentication = this.username + ":" + this.password;
    String encodedAuthentication = Base64.encodeToString(authentication.getBytes(), Base64.NO_WRAP);
    request.addHeader("Authorization", "Basic " + encodedAuthentication);
}

From source file:org.andstatus.app.net.HttpConnectionBasic.java

/**
 * Get the HTTP digest authentication. Uses Base64 to encode credentials.
 * //from   w  w  w  .  j a v a  2  s  .  c o m
 * @return String
 */
private String getCredentials() {
    // TODO: since API9 we will use getBytes(Charset.forName("US-ASCII"))
    return Base64.encodeToString((data.accountUsername + ":" + mPassword).getBytes(),
            Base64.NO_WRAP + Base64.NO_PADDING);
}

From source file:com.android.exchange.service.EasServerConnection.java

private String makeAuthString() {
    final String cs = mHostAuth.mLogin + ":" + mHostAuth.mPassword;
    return "Basic " + Base64.encodeToString(cs.getBytes(), Base64.NO_WRAP);
}

From source file:com.oakesville.mythling.util.MediaStreamProxy.java

private HttpResponse download() throws IOException {

    httpClient = AndroidHttpClient.newInstance("Android");
    // httpClient.getParams().setParameter(ClientPNames.VIRTUAL_HOST, new HttpHost("127.0.0.1"));

    URL netUrl = proxyInfo.netUrl;
    HttpHost host = new HttpHost(netUrl.getHost(), netUrl.getPort(), netUrl.getProtocol());

    HttpRequestBase request = new HttpGet(netUrl.toString());
    HttpResponse response = null;//w ww.  j  av a2  s .  co  m
    Log.d(TAG, "Proxy starting download");
    if (authType == AuthType.Digest) {
        HttpContext context = HttpHelper.getDigestAuthContext(netUrl.getHost(), netUrl.getPort(),
                proxyInfo.user, proxyInfo.password);
        response = httpClient.execute(host, request, context);
    } else if (authType == AuthType.Basic) {
        String credentials = Base64.encodeToString((proxyInfo.user + ":" + proxyInfo.password).getBytes(),
                Base64.DEFAULT);
        request.setHeader("Authorization", "Basic " + credentials);
        response = httpClient.execute(host, request);
    } else {
        response = httpClient.execute(host, request);
    }
    Log.d(TAG, "Proxy response downloaded");
    return response;
}

From source file:de.treichels.hott.ui.android.html.AndroidCurveImageGenerator.java

@Override
public String getImageSource(final Curve curve, final float scale, final boolean description) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    getBitmap(curve, scale, description).compress(CompressFormat.PNG, 100, baos);
    return PREFIX + Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
}

From source file:at.diamonddogs.util.Utils.java

/**
 * Byte array to base64//ww w  . j  a va 2 s  .c om
 *
 * @param inMsg the message to be converted to base64
 * @return the base64 string
 */
public static String encrypt(byte[] inMsg) {
    return Base64.encodeToString(inMsg, Base64.DEFAULT);
}