Example usage for android.net Uri encode

List of usage examples for android.net Uri encode

Introduction

In this page you can find the example usage for android.net Uri encode.

Prototype

public static String encode(String s) 

Source Link

Document

Encodes characters in the given string as '%'-escaped octets using the UTF-8 scheme.

Usage

From source file:Main.java

/**
 * method used to lookup the id of a contact photo id
 *
 * @param context//from   w  w  w.  java  2 s .c o  m
 *            a context object used to get a content resolver
 * @param phoneNumber
 *            the phone number of the contact
 *
 * @return the id of the contact
 */
public static long lookupPhotoId(Context context, String phoneNumber) {

    long mPhotoId = -1;

    Uri mLookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

    String[] mProjection = new String[2];
    mProjection[0] = PhoneLookup._ID;
    mProjection[1] = PhoneLookup.PHOTO_ID;

    Cursor mCursor = context.getContentResolver().query(mLookupUri, mProjection, null, null, null);

    if (mCursor.getCount() > 0) {
        mCursor.moveToFirst();

        mPhotoId = mCursor.getLong(mCursor.getColumnIndex(PhoneLookup._ID));

        mCursor.close();
    }

    return mPhotoId;
}

From source file:Main.java

public static void appendPropertyAsUrlArgs(StringBuilder builder, String key, String value) {
    if (builder != null && !TextUtils.isEmpty(key)) {
        builder.append(key).append(URL_EQUAL).append(Uri.encode(value));
    }// w  w  w  .  j  a  va2  s .c  om
}

From source file:com.parse.ParseRESTAnalyticsCommand.java

static ParseRESTAnalyticsCommand trackEventCommand(String eventName, String pushHash,
        Map<String, String> dimensions, String sessionToken) {
    String httpPath = String.format(PATH, Uri.encode(eventName));
    JSONObject parameters = new JSONObject();
    try {/*from  ww w . j a va  2  s .  com*/
        parameters.put(KEY_AT, NoObjectsEncoder.get().encode(new Date()));
        if (pushHash != null) {
            parameters.put(KEY_PUSH_HASH, pushHash);
        }
        if (dimensions != null) {
            parameters.put(KEY_DIMENSIONS, NoObjectsEncoder.get().encode(dimensions));
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    return new ParseRESTAnalyticsCommand(httpPath, ParseHttpRequest.Method.POST, parameters, sessionToken);
}

From source file:com.parse.ParseRESTObjectCommand.java

private static ParseRESTObjectCommand createObjectCommand(String className, JSONObject changes,
        String sessionToken) {/*from  w  ww.  ja v  a 2 s.  c  o m*/
    String httpPath = String.format("classes/%s", Uri.encode(className));
    return new ParseRESTObjectCommand(httpPath, ParseHttpRequest.Method.POST, changes, sessionToken);
}

From source file:Main.java

/**
 * Create a URL from an API call (or "action string")
 *//*from  w  w  w  .j a  v a 2 s.  co m*/
public static String getPageURL(String call) {
    Log.i("Jarvis", String.format("Call: %s", call));

    // Call's parts
    String function = "";
    String action = "";
    String data = "";

    // Final URL
    String url = "";
    Boolean isInternal = true;

    // This is weird left-over support for external URLs
    // Check if this is not a Jarvis URL
    if (call.length() >= 4 && call.substring(0, 4).equals("http")) {
        Integer length = API_ROOT.length();
        if (call.substring(0, length).equals(API_ROOT)) {
            call = call.substring(length + 1);
            call = call.replace("/", " ");
        } else {
            url = call;
            isInternal = false;
        }
    }

    if (isInternal) {
        // Encode URI data
        Pattern pattern = Pattern.compile(" ");
        String[] parts = pattern.split(call, 3);
        function = Uri.encode(parts[0]);
        if (parts.length > 1) {
            action = Uri.encode(parts[1]);
        }
        if (parts.length > 2) {
            data = Uri.encode(parts[2]);
        }

        // Final format
        url = String.format("%s/api/%s/%s/%s", API_ROOT, function, action, data);
    }

    Log.d("Jarvis", String.format("URL: %s", url));

    return url;
}

From source file:com.parse.ParseRESTObjectCommand.java

private static ParseRESTObjectCommand updateObjectCommand(String objectId, String className, JSONObject changes,
        String sessionToken) {/*from   w  ww.j  a  v  a 2  s . co m*/
    String httpPath = String.format("classes/%s/%s", Uri.encode(className), Uri.encode(objectId));
    return new ParseRESTObjectCommand(httpPath, ParseHttpRequest.Method.PUT, changes, sessionToken);
}

From source file:net.kourlas.voipms_sms.Utils.java

/**
 * Gets the name of a contact from the Android contacts provider, given a phone number.
 *
 * @param applicationContext The application context.
 * @param phoneNumber        The phone number of the contact.
 * @return the name of the contact./*w  w  w  .  j av  a 2 s .  c  om*/
 */
public static String getContactName(Context applicationContext, String phoneNumber) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = applicationContext.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME }, null,
            null, null);
    if (cursor.moveToFirst()) {
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
        cursor.close();
        return name;
    } else {
        cursor.close();
        return null;
    }
}

From source file:com.parse.ParseRESTObjectCommand.java

public static ParseRESTObjectCommand deleteObjectCommand(ParseObject.State state, String sessionToken) {
    String httpPath = String.format("classes/%s", Uri.encode(state.className()));
    String objectId = state.objectId();
    if (objectId != null) {
        httpPath += String.format("/%s", Uri.encode(objectId));
    }/*from   ww w . ja v a2 s. c om*/
    return new ParseRESTObjectCommand(httpPath, ParseHttpRequest.Method.DELETE, null, sessionToken);
}

From source file:ph.sakay.gateway.RoutingService.java

private String request(String sender, String message) {
    try {//from   www  .  jav  a  2s  . c  o  m
        Log.d("RoutingService", "Querying server");
        HttpResponse response = new APIClient(this).get("/sms", "?target=" + Uri.encode(getOwnNumber())
                + "&body=" + Uri.encode(message) + "&source=" + Uri.encode(sender));
        int status = response.getStatusLine().getStatusCode();
        String body = EntityUtils.toString(response.getEntity());
        if (status >= 200 && status < 300) {
            Log.d("RoutingService", "Got server response: " + body);
            return body;
        } else {
            Log.e("RoutingService",
                    "Error while querying server. Got status: " + status + " and response: " + body);
            return "Sorry, there is a problem with the server.";
        }
    } catch (Exception e) {
        Log.d("RoutingService", "Error while querying server.", e);
        return "Sorry, an error has occurred.";
    }
}

From source file:com.eugene.fithealthmaingit.FatSecretSearchAndGet.FatSecretSearchMethod.java

public JSONObject searchFood(String searchFood, int page) {
    Log.e("Search", searchFood);
    List<String> params = new ArrayList<>(Arrays.asList(generateOauthParams(page)));
    String[] template = new String[1];
    params.add("method=foods.search");
    params.add("search_expression=" + Uri.encode(searchFood));
    params.add("oauth_signature=" + sign(Globals.APP_METHOD, Globals.APP_URL, params.toArray(template)));
    JSONObject foods = null;/*  w w w .ja  va 2 s. c  om*/
    try {
        URL url = new URL(Globals.APP_URL + "?" + paramify(params.toArray(template)));
        URLConnection api = url.openConnection();
        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(api.getInputStream()));
        while ((line = reader.readLine()) != null)
            builder.append(line);
        JSONObject food = new JSONObject(builder.toString()); // { first
        foods = food.getJSONObject("foods"); //second
    } catch (Exception exception) {
        Log.e("FatSecret Error", exception.toString());
        exception.printStackTrace();
    }
    return foods;
}