Eclipse Public License - v 1.0
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM
CONSTITUTES RECI...
If you think the Android project doubanbook4android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* UriUtils.java//fromwww.java2s.com
* classes : org.kevin.douban.util.UriUtils
* Created at : 2013-5-9 ????5:51:39
*/package com.study.doubanbook_for_android.utils;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import com.study.doubanbook_for_android.auth.DoubanParameters;
import android.os.Bundle;
import android.util.Log;
/**
* @author thinkfeed#gmail.com
*
*/publicclass UriUtils {
publicstatic String encodeUrl(DoubanParameters parameters) {
if (parameters == null) {
return"";
}
StringBuilder sb = new StringBuilder();
boolean first = true;
for (int loc = 0; loc < parameters.size(); loc++) {
if (first) {
first = false;
} else {
sb.append("&");
}
String _key = parameters.getKey(loc);
String _value = parameters.getValue(_key);
if (_value == null) {
Log.i("encodeUrl", "key:" + _key + " 's value is null");
} else {
try {
sb.append(URLEncoder.encode(parameters.getKey(loc), "utf-8")
+ "="
+ URLEncoder.encode(parameters.getValue(loc),
"utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return sb.toString();
}
publicstatic String encodeParameters(DoubanParameters httpParams) {
if (null == httpParams || isBundleEmpty(httpParams)) {
return"";
}
StringBuilder buf = new StringBuilder();
int j = 0;
for (int loc = 0; loc < httpParams.size(); loc++) {
String key = httpParams.getKey(loc);
if (j != 0) {
buf.append("&");
}
try {
buf.append(URLEncoder.encode(key, "UTF-8"))
.append("=")
.append(URLEncoder.encode(httpParams.getValue(key),
"UTF-8"));
} catch (java.io.UnsupportedEncodingException neverHappen) {
}
j++;
}
return buf.toString();
}
privatestaticboolean isBundleEmpty(DoubanParameters bundle) {
if (bundle == null || bundle.size() == 0) {
return true;
}
return false;
}
publicstatic Bundle parseUrl(String url) {
try {
URL u = new URL(url);
Bundle b = decodeUrl(u.getQuery());
b.putAll(decodeUrl(u.getRef()));
return b;
} catch (MalformedURLException e) {
returnnew Bundle();
}
}
publicstatic Bundle decodeUrl(String s) {
Bundle params = new Bundle();
if (s != null) {
String array[] = s.split("&");
for (String parameter : array) {
String v[] = parameter.split("=");
try {
params.putString(URLDecoder.decode(v[0], "utf-8"),
URLDecoder.decode(v[1], "utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return params;
}
}