本文整理汇总了Java中java.net.URLEncoder类的典型用法代码示例。如果您正苦于以下问题:Java URLEncoder类的具体用法?Java URLEncoder怎么用?Java URLEncoder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
URLEncoder类属于java.net包,在下文中一共展示了URLEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUrl
import java.net.URLEncoder; //导入依赖的package包/类
/**
* Sets the URL to request.
* @param url The URL without query
* @param params A map that contains URL query parameters into key and value. The key and value
* will be percent encoded as UTF-8.
* @throws UnsupportedEncodingException Occurs when UTF-8 is not available.
* @since v1.1.0
*/
@NotNull
public HttpRequester setUrl(@NotNull String url, @NotNull Map<String, String> params)
throws UnsupportedEncodingException
{
StringBuilder queryString = new StringBuilder();
// Convert the params map into a query string.
for (Map.Entry<String, String> entry : params.entrySet())
{
if (queryString.length() != 0)
queryString.append('&');
String encodedKey = URLEncoder.encode(entry.getKey(), "UTF-8");
String encodedValue = URLEncoder.encode(entry.getValue(), "UTF-8");
queryString.append(encodedKey);
queryString.append('=');
queryString.append(encodedValue);
}
return setUrl(url + '?' + queryString.toString());
}
示例2: searchKeywordNextPage
import java.net.URLEncoder; //导入依赖的package包/类
private void searchKeywordNextPage(String keyword) {
try {
String unitStr = URLEncoder.encode(keyword, "utf8"); //字體要utf8編碼
StringBuilder sb = new StringBuilder(ConfigUtil.GOOGLE_SEARCH_API);
sb.append("location=" + mLatitude + "," + mLongitude);
sb.append("&radius=" + radius);
sb.append("&language =" + language);
sb.append("&types=" + keyword);
sb.append("&sensor=true");
sb.append("&key=" + ConfigUtil.API_KEY_GOOGLE_MAP); //server key
sb.append("&pagetoken=" + page_token);
MainActivity.PlacesTask placesTask = new MainActivity.PlacesTask(MainActivity.this);
Log.v(TAG, sb.toString());
placesTask.execute(sb.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.i(ConfigUtil.TAG, "Exception:" + e);
}
}
示例3: provideURL
import java.net.URLEncoder; //导入依赖的package包/类
private URL provideURL(String[] coords) throws UnsupportedEncodingException, MalformedURLException {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String apiKey = sp.getString("apiKey", activity.getResources().getString(R.string.apiKey));
StringBuilder urlBuilder = new StringBuilder("http://api.openweathermap.org/data/2.5/");
urlBuilder.append(getAPIName()).append("?");
if (coords.length == 2) {
urlBuilder.append("lat=").append(coords[0]).append("&lon=").append(coords[1]);
} else {
final String city = sp.getString("city", Constants.DEFAULT_CITY);
urlBuilder.append("q=").append(URLEncoder.encode(city, "UTF-8"));
}
urlBuilder.append("&lang=").append(getLanguage());
urlBuilder.append("&mode=json");
urlBuilder.append("&appid=").append(apiKey);
return new URL(urlBuilder.toString());
}
示例4: convertMapToString
import java.net.URLEncoder; //导入依赖的package包/类
static String convertMapToString(Map<String,String> maps){
StringBuilder stringBuilder = new StringBuilder();
for (String key : maps.keySet()) {
if (stringBuilder.length() > 0) {
stringBuilder.append("&");
}
String value = maps.get(key);
try {
stringBuilder.append((key != null ? URLEncoder.encode(key, "UTF-8") : ""));
stringBuilder.append("=");
stringBuilder.append(value != null ? URLEncoder.encode(value, "UTF-8") : "");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("This method requires UTF-8 encoding support", e);
}
}
return stringBuilder.toString();
}
示例5: index
import java.net.URLEncoder; //导入依赖的package包/类
@ApiOperation(value = "认证中心首页")
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(HttpServletRequest request) throws Exception {
String appid = request.getParameter("appid");
String backurl = request.getParameter("backurl");
if (StringUtils.isBlank(appid)) {
throw new RuntimeException("无效访问!");
}
// 判断请求认证系统是否注册
UpmsSystemExample upmsSystemExample = new UpmsSystemExample();
upmsSystemExample.createCriteria()
.andNameEqualTo(appid);
int count = upmsSystemService.countByExample(upmsSystemExample);
if (0 == count) {
throw new RuntimeException(String.format("未注册的系统:%s", appid));
}
return "redirect:/sso/login?backurl=" + URLEncoder.encode(backurl, "utf-8");
}
示例6: getQuery
import java.net.URLEncoder; //导入依赖的package包/类
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
if(pair!=null&&pair.getName()!=null&&pair.getValue()!=null){
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
}
return result.toString();
}
示例7: getLoginURL
import java.net.URLEncoder; //导入依赖的package包/类
public static String getLoginURL(String url)
{
String _url = loginURL;
try
{
if(url == null || url.length() == 0)
{
if(systemURL.length() > 0)
{
_url = loginURL + "?service=" + URLEncoder.encode(systemURL, "UTF-8");
}
}
else
{
_url = loginURL + "?service=" + URLEncoder.encode(url, "UTF-8");
}
}
catch(Exception e)
{
_url = loginURL;
}
return _url;
}
示例8: encodeParameters
import java.net.URLEncoder; //导入依赖的package包/类
public static String encodeParameters(PostParameter[] httpParams) {
if (null == httpParams) {
return "";
}
StringBuffer buf = new StringBuffer();
for (int j = 0; j < httpParams.length; j++) {
if (httpParams[j].isFile()) {
throw new IllegalArgumentException("parameter [" + httpParams[j].name + "]should be text");
}
if (j != 0) {
buf.append("&");
}
try {
buf.append(URLEncoder.encode(httpParams[j].name, "UTF-8"))
.append("=").append(URLEncoder.encode(httpParams[j].value, "UTF-8"));
} catch (java.io.UnsupportedEncodingException neverHappen) {
}
}
return buf.toString();
}
示例9: generatePlaylist
import java.net.URLEncoder; //导入依赖的package包/类
static String generatePlaylist(String contentJson, JsonObject channelsJson, String host, int port) {
JsonArray neterraContentArray = new JsonParser().parse(contentJson).getAsJsonObject()
.get("tv_choice_result").getAsJsonArray();
StringBuilder m3u8 = new StringBuilder("#EXTM3U\n");
for (int i = 0; i < neterraContentArray.size(); i++) {
JsonObject channel = neterraContentArray.get(i).getAsJsonArray().get(0).getAsJsonObject();
String chanId = channel.get("issues_id").getAsString();
String chanName = channel.get("issues_name").getAsString();
String tvgId = "";
String tvgName = "";
String group = "";
String logo = "";
JsonObject definedChannel = channelsJson.getAsJsonObject(chanId);
if (definedChannel != null) {
chanName = definedChannel.get("name").getAsString();
tvgId = definedChannel.get("tvg-id").getAsString();
tvgName = definedChannel.get("tvg-name").getAsString();
group = definedChannel.get("group").getAsString();
logo = definedChannel.get("logo").getAsString();
}
String encodedChanName = null;
try {
encodedChanName = URLEncoder.encode(chanName, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
m3u8.append(String.format("#EXTINF:-1 tvg-id=\"%s\" tvg-name=\"%s\" tvg-logo=\"%s\" " +
"group-title=\"%s\",%s\nhttp://%s:%s/playlist.m3u8?ch=%s&name=%s\n",
tvgId, tvgName, logo, group, chanName, host, port, chanId, encodedChanName));
}
return m3u8.toString();
}
示例10: getParamsString
import java.net.URLEncoder; //导入依赖的package包/类
private final String getParamsString(Hashtable excludeParams, HttpServletRequest request) {
if (request == null)
return null;
Enumeration params = request.getParameterNames();
String param;
String vals[];
StringBuffer addParams = new StringBuffer();
try {
while (params.hasMoreElements()) {
param = (String) params.nextElement();
if (!excludeParams.containsKey(param)) {
vals = request.getParameterValues(param);
for (int i = 0; i < vals.length; i++) {
addParams.append("&" + param + "=" + URLEncoder.encode(vals[i], "utf-8"));
}
}
}
} catch (Exception e) {
addParams.toString();
}
return addParams.toString();
}
示例11: encodeUri
import java.net.URLEncoder; //导入依赖的package包/类
/**
* URL-encodes everything between "/"-characters.
* Encodes spaces as '%20' instead of '+'.
*/
public static String encodeUri( String uri )
{
String newUri = "";
StringTokenizer st = new StringTokenizer( uri, "/ ", true );
while ( st.hasMoreTokens())
{
String tok = st.nextToken();
if ( tok.equals( "/" ))
newUri += "/";
else if ( tok.equals( " " ))
newUri += "%20";
else
{
newUri += URLEncoder.encode( tok );
// For Java 1.4 you'll want to use this instead:
// try { newUri += URLEncoder.encode( tok, "UTF-8" ); } catch ( UnsupportedEncodingException uee )
}
}
return newUri;
}
示例12: buildQuery
import java.net.URLEncoder; //导入依赖的package包/类
public static String buildQuery(Map<String, String> params, String charset) throws IOException {
if (params == null || params.isEmpty()) {
return null;
}
StringBuilder query = new StringBuilder();
Set<Entry<String, String>> entries = params.entrySet();
boolean hasParam = false;
for (Entry<String, String> entry : entries) {
String name = entry.getKey();
String value = entry.getValue();
// 忽略参数名或参数值为空的参数
if (StringUtils.areNotEmpty(name, value)) {
if (hasParam) {
query.append("&");
} else {
hasParam = true;
}
query.append(name).append("=").append(URLEncoder.encode(value, charset));
}
}
return query.toString();
}
示例13: getPostDataString
import java.net.URLEncoder; //导入依赖的package包/类
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
示例14: sendLogRequest
import java.net.URLEncoder; //导入依赖的package包/类
/**
* 开始解析发送参数
*/
private void sendLogRequest(Request request) throws IOException {
if (request != null) {
String body = "";
if (request.body() != null) {
Buffer buffer = new Buffer();
request.body().writeTo(buffer);
//编码设为UTF-8
Charset charset = Charset.forName("UTF-8");
MediaType contentType = request.body().contentType();
if (contentType != null) {
charset = contentType.charset(Charset.forName("UTF-8"));
}
body = buffer.readString(charset);
//如果你出现中文参数乱码情况,请进行URL解码处理!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
body = URLEncoder.encode(body, "UTF-8");
}
LogUtils.i("发送----" + "method:" + request.method() + " url:" + request.url() + " body:" + body);
}
}
示例15: encodeParameters
import java.net.URLEncoder; //导入依赖的package包/类
public static String encodeParameters(HttpParameter[] httpParameters){
if (null == httpParameters) { return ""; }
StringBuilder paramBuff = new StringBuilder();
for (int i = 0; i < httpParameters.length; i++){
try {
paramBuff.append(URLEncoder.encode(httpParameters[i].getName(), "UTF-8"))
.append("=")
.append(URLEncoder.encode(httpParameters[i].getValue(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (i != httpParameters.length - 1){
paramBuff.append("&");
}
}
return paramBuff.toString();
}