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:com.nextgis.firereporter.ScanexHttpLogin.java

@Override
protected Void doInBackground(String... urls) {
    if (HttpGetter.IsNetworkAvailible(mContext)) {
        String sUser = urls[0];// ww  w  .  j  a  va 2  s .  c o  m
        String sPass = urls[1];

        try {

            // Create a new HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // step 1. open login dialog
            String sRedirect = "http://fires.kosmosnimki.ru/SAPI/oAuthCallback.html&authServer=MyKosmosnimki";
            String sURL = "http://fires.kosmosnimki.ru/SAPI/LoginDialog.ashx?redirect_uri="
                    + Uri.encode(sRedirect);
            HttpGet httpget = new HttpGet(sURL);
            HttpParams params = httpget.getParams();
            params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
            httpget.setParams(params);
            HttpResponse response = httpclient.execute(httpget);

            //2 get cookie and params            
            Header head = response.getFirstHeader("Set-Cookie");
            String sCookie = head.getValue();

            head = response.getFirstHeader("Location");
            String sLoc = head.getValue();

            Uri uri = Uri.parse(sLoc);
            String sClientId = uri.getQueryParameter("client_id");
            String sScope = uri.getQueryParameter("scope");
            String sState = uri.getQueryParameter("state");

            String sPostUri = "http://my.kosmosnimki.ru/Account/LoginDialog?redirect_uri="
                    + Uri.encode(sRedirect) + "&client_id=" + sClientId + "&scope=" + sScope + "&state="
                    + sState;

            HttpPost httppost = new HttpPost(sPostUri);

            params = httppost.getParams();
            params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
            httppost.setHeader("Cookie", sCookie);

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("email", sUser));
            nameValuePairs.add(new BasicNameValuePair("password", sPass));
            nameValuePairs.add(new BasicNameValuePair("IsApproved", "true"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            response = httpclient.execute(httppost);
            head = response.getFirstHeader("Set-Cookie");

            if (head == null) {
                mError = mContext.getString(R.string.noNetwork);
                return null;
            }

            sCookie += "; " + head.getValue();
            head = response.getFirstHeader("Location");
            sLoc = head.getValue();

            uri = Uri.parse(sLoc);
            String sCode = uri.getQueryParameter("code");
            sState = uri.getQueryParameter("state");

            //3 get 
            String sGetUri = "http://fires.kosmosnimki.ru/SAPI/Account/logon/?authServer=MyKosmosnimki&code="
                    + sCode + "&state=" + sState;
            httpget = new HttpGet(sGetUri);
            httpget.setHeader("Cookie", sCookie);
            params = httpget.getParams();
            params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
            httpget.setParams(params);
            response = httpclient.execute(httpget);

            head = response.getFirstHeader("Set-Cookie");
            if (head == null) {
                mError = mContext.getString(R.string.noNetwork);
                return null;
            }
            sCookie += "; " + head.getValue();

            Bundle bundle = new Bundle();
            if (sCookie.length() > 0) {
                //if(bGetCookie){
                bundle.putBoolean(GetFiresService.ERROR, false);
                bundle.putString(GetFiresService.JSON, sCookie);
                //bundle.putString("json", mContent);
            } else {
                bundle.putBoolean(GetFiresService.ERROR, true);
            }

            bundle.putInt(GetFiresService.SOURCE, mnType);
            Message msg = new Message();
            msg.setData(bundle);
            if (mEventReceiver != null) {
                mEventReceiver.sendMessage(msg);
            }

        } catch (ClientProtocolException e) {
            mError = e.getMessage();
            return null;
        } catch (IOException e) {
            mError = e.getMessage();
            return null;
        } catch (Exception e) {
            mError = e.getMessage();
            return null;
        }
    } else {
        Bundle bundle = new Bundle();
        bundle.putBoolean(GetFiresService.ERROR, true);
        bundle.putString(GetFiresService.ERR_MSG, mContext.getString(R.string.noNetwork));
        bundle.putInt(GetFiresService.SOURCE, mnType);

        Message msg = new Message();
        msg.setData(bundle);
        if (mEventReceiver != null) {
            mEventReceiver.sendMessage(msg);
        }
    }
    return null;
}

From source file:uk.co.seesaw.android.whatsonseesaw.SeesawSeachHelper.java

private static List<SearchResult> fetchResults(String title) throws ApiException, ParseException {
    // Encode page title and expand templates if requested
    String encodedTitle = Uri.encode(title);

    // Query the API for content
    String content = getUrlContent(String.format(SEESAW_SEARCH_PAGE, encodedTitle));
    try {/*from  w w  w. j a v a 2s .  co m*/
        // Drill into the JSON response to find the content body
        JSONObject response = new JSONObject(content);
        String htmlList = response.getString("content");

        Matcher matcher = pattern.matcher(content);
        List<SearchResult> searchResults = new ArrayList<SearchResult>();
        while (matcher.find()) {
            String resultUrl = matcher.group(1);
            String resultTitle = matcher.group(2);
            SearchResult searchResult = new SearchResult(resultTitle, resultUrl);
            searchResults.add(searchResult);
        }

        return searchResults;
    } catch (JSONException e) {
        throw new ParseException("Problem parsing API response", e);
    }
}

From source file:org.thoughtcrime.securesms.contacts.ContactAccessorNewApi.java

@Override
public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) {
    String phone = "";
    String cons = null;//  ww  w . j av  a 2  s .c o  m
    if (constraint != null) {
        cons = constraint.toString();

        if (RecipientsAdapter.usefulAsDigits(cons)) {
            phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
            if (phone.equals(cons)) {
                phone = "";
            } else {
                phone = phone.trim();
            }
        }
    }

    Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons));
    String selection = String.format("%s=%s OR %s=%s OR %s=%s", Phone.TYPE, Phone.TYPE_MOBILE, Phone.TYPE,
            Phone.TYPE_WORK_MOBILE, Phone.TYPE, Phone.TYPE_MMS);

    Cursor phoneCursor = mContentResolver.query(uri, PROJECTION_PHONE, null, null, SORT_ORDER);

    if (phone.length() > 0) {
        ArrayList result = new ArrayList();
        result.add(Integer.valueOf(-1)); // ID
        result.add(Long.valueOf(-1)); // CONTACT_ID
        result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE
        result.add(phone); // NUMBER

        /*
         * The "\u00A0" keeps Phone.getDisplayLabel() from deciding
         * to display the default label ("Home") next to the transformation
         * of the letters into numbers.
         */
        result.add("\u00A0"); // LABEL
        result.add(cons); // NAME

        ArrayList<ArrayList> wrap = new ArrayList<ArrayList>();
        wrap.add(result);

        ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap);

        return new MergeCursor(new Cursor[] { translated, phoneCursor });
    } else {
        return phoneCursor;
    }

}

From source file:com.gsma.rcs.ri.utils.RcsContactUtil.java

private String getDisplayNameFromAddressBook(ContactId contact) {
    /* First try to get it from cache */
    String displayName = mDisplayNameAndroidCache.get(contact);
    if (displayName != null) {
        return displayName;
    }// ww  w  .  j  av a  2 s .  c  o  m
    /* Not found in cache: query the Android address book */
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(contact.toString()));
    Cursor cursor = null;
    try {
        cursor = mResolver.query(uri, PROJ_DISPLAY_NAME, null, null, null);
        if (cursor == null) {
            throw new SQLException("Cannot query display name for contact=" + contact);
        }
        if (!cursor.moveToFirst()) {
            return null;
        }
        displayName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
        /* Insert in cache */
        mDisplayNameAndroidCache.put(contact, displayName);
        return displayName;

    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:com.photobox.android.iris.MyFirebaseMessagingService.java

private void makeCall(String number) {
    final Intent dial = new Intent(Intent.ACTION_CALL);
    dial.setData(Uri.parse("tel:" + Uri.encode(number)));
    //this.getApplicationContext().startActivity(dial);
}

From source file:org.xbmc.android.remotesandbox.ui.AlbumsFragment.java

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // This is called when a new Loader needs to be created. This
    // sample only has one Loader, so we don't care about the ID.
    // First, pick the base URI to use depending on whether we are
    // currently filtering.
    Uri baseUri;//from  w w  w .  ja  v  a2s  .  c o m
    if (mCurrentFilter != null) {
        baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurrentFilter));
    } else {
        baseUri = Contacts.CONTENT_URI;
    }

    return new CursorLoader(getActivity(), AudioContract.Albums.CONTENT_URI, AlbumsQuery.PROJECTION, null, null,
            AudioContract.Albums.DEFAULT_SORT);
}

From source file:com.dmplayer.childfragment.ChildFragmentGenres.java

private Cursor getGenersCursor(AsyncQueryHandler async, String filter) {

    String[] cols = new String[] { MediaStore.Audio.Genres._ID, MediaStore.Audio.Genres.NAME };

    Uri uri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
    if (!TextUtils.isEmpty(filter)) {
        uri = uri.buildUpon().appendQueryParameter("filter", Uri.encode(filter)).build();
    }//from   w ww .j  a  va 2s .  c om

    Cursor ret = null;
    if (async != null) {
        async.startQuery(0, null, uri, cols, null, null, null);
    } else {
        ret = DMPlayerUtility.query(getActivity(), uri, cols, null, null, null);
    }
    return ret;
}

From source file:org.xbmc.android.remotesandbox.ui.ArtistsFragment.java

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // This is called when a new Loader needs to be created. This
    // sample only has one Loader, so we don't care about the ID.
    // First, pick the base URI to use depending on whether we are
    // currently filtering.
    Uri baseUri;//from  w  w  w  .java  2  s .  c  o  m
    if (mCurrentFilter != null) {
        baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurrentFilter));
    } else {
        baseUri = Contacts.CONTENT_URI;
    }

    return new CursorLoader(getActivity(), AudioContract.Artists.CONTENT_URI, ArtistsQuery.PROJECTION, null,
            null, AudioContract.Artists.DEFAULT_SORT);
}

From source file:co.codecrunch.musicplayerlite.childfragment.ChildFragmentGenres.java

private Cursor getGenersCursor(AsyncQueryHandler async, String filter) {

    String[] cols = new String[] { MediaStore.Audio.Genres._ID, MediaStore.Audio.Genres.NAME };

    Uri uri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
    if (!TextUtils.isEmpty(filter)) {
        uri = uri.buildUpon().appendQueryParameter("filter", Uri.encode(filter)).build();
    }//from  w w  w .  j  a v  a  2s . co  m

    Cursor ret = null;
    if (async != null) {
        async.startQuery(0, null, uri, cols, null, null, null);
    } else {
        ret = MusicPlayerUtility.query(getActivity(), uri, cols, null, null, null);
    }
    return ret;
}

From source file:com.dmplayer.childfragment.ChildFragmentArtists.java

private Cursor getArtistCursor(AsyncQueryHandler async, String filter) {

    String[] cols = new String[] { MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST,
            MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, MediaStore.Audio.Artists.NUMBER_OF_TRACKS };

    Uri uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
    if (!TextUtils.isEmpty(filter)) {
        uri = uri.buildUpon().appendQueryParameter("filter", Uri.encode(filter)).build();
    }/*from  w w w.j ava  2s . c om*/

    Cursor ret = null;
    if (async != null) {
        async.startQuery(0, null, uri, cols, null, null, MediaStore.Audio.Artists.ARTIST_KEY);
    } else {
        ret = DMPlayerUtility.query(getActivity(), uri, cols, null, null, MediaStore.Audio.Artists.ARTIST_KEY);
    }
    return ret;
}