Android URL Create createUrl(final String rootUrl, final String indivisualHost, final String featurePath, final int index, List pairs)

Here you can find the source of createUrl(final String rootUrl, final String indivisualHost, final String featurePath, final int index, List pairs)

Description

create Url

Declaration

public static String createUrl(final String rootUrl,
            final String indivisualHost, final String featurePath,
            final int index, List<NameValuePair> pairs) 

Method Source Code

//package com.java2s;
import java.net.URI;

import java.util.List;
import java.util.Random;
import org.apache.http.NameValuePair;

import android.net.Uri;
import android.util.Log;

public class Main {
    public static String createUrl(final String rootUrl,
            final String indivisualHost, final String featurePath,
            final int index, List<NameValuePair> pairs) {
        Uri.Builder builder = new Uri.Builder();
        try {//from  w w w  .j a va  2 s.  c o  m
            URI uri = new URI(rootUrl);
            String urlQuery = createQueryUrl(indivisualHost, featurePath,
                    index, pairs);

            builder.scheme(uri.getScheme());
            builder.authority(uri.getAuthority());
            builder.path(uri.getPath() + "/");
            builder.appendQueryParameter("guid", "ON");
            builder.appendQueryParameter("url", urlQuery);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return builder.build().toString();
    }

    public static String createQueryUrl(final String indivisualHost,
            final String featurePath, final int index,
            List<NameValuePair> pairs) {
        String path = featurePath;
        if (path.endsWith("/"))
            path += "/";
        if (index > 0) {
            path = path + Integer.toString(index);
        }

        Uri.Builder builder = new Uri.Builder();

        builder.scheme("http");
        builder.authority(indivisualHost);
        builder.path(path);
        for (NameValuePair pair : pairs) {
            builder.appendQueryParameter(pair.getName(), pair.getValue());
        }
        builder.appendQueryParameter("rnd",
                Integer.toString(new Random().nextInt(100000000)));

        try {
            String uri = builder.build().toString();
            Log.d("MbgaUtils", "QueryUrl: " + uri);
            URI uriQuery = new URI(uri);
            return uriQuery.toASCIIString();
        } catch (Exception e) {
            // TODO: handle exception
            Log.d("MbgaUtils", e.getMessage());
            return "";
        }
    }
}

Related

  1. mapToQueryString( HashMap queryString)
  2. buildQueries(String baseQuery, List datesByQueryIndex)
  3. buildEmailUri(String email, String subject, CharSequence body)
  4. url(String baseUrl, String relativePath)
  5. url(String baseUrl, String relativePath)
  6. buildBodyParameterString( List> parameters)
  7. buildParameterString( List> parameters, String delimiter, boolean quote)
  8. buildUrlParameterString( List> parameters)
  9. appendParameter(String url, String name, String value)