com.pubnub.api.PubnubUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pubnub-android-debug Show documentation
Show all versions of pubnub-android-debug Show documentation
PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter second!
The newest version!
package com.pubnub.api;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Locale;
/**
* PubnubUtil class provides utility methods like urlEncode etc
*
* @author Pubnub
*
*/
public class PubnubUtil extends PubnubUtilCore {
public static String stringEscapeSlashes(String s, String a, String b) {
return s.replace(a, b);
}
public static String stringReplaceAll(String s, String a, String b) {
return s.replaceAll(a, b);
}
/**
* Returns encoded String
*
* @param sUrl
* , input string
* @return , encoded string
*/
public static String pamEncode(String sUrl) {
/* !'()*~ */
String encoded = urlEncode(sUrl);
if (encoded != null) {
encoded = encoded.replace("*", "%2A").replace("!", "%21").replace("'", "%27").replace("(", "%28")
.replace(")", "%29").replace("[", "%5B").replace("]", "%5D").replace("~", "%7E");
}
return encoded;
}
/**
* Returns encoded String
*
* @param sUrl
* , input string
* @return , encoded string
*/
public static String urlEncode(String sUrl) {
try {
return URLEncoder.encode(sUrl, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
return null;
}
}
/**
* Convert input String to JSONObject, JSONArray, or String
*
* @param str
* JSON data in string format
*
* @return JSONArray or JSONObject or String
*/
static Object stringToJSON(String str) {
try {
return new JSONArray(str);
} catch (JSONException e) {
}
try {
return new JSONObject(str);
} catch (JSONException ex) {
}
try {
return Integer.parseInt(str);
} catch (Exception ex) {
}
try {
return Double.parseDouble(str);
} catch (Exception ex) {
}
return str;
}
/**
* Takes source and delimiter string as inputs and returns splitted string
* in form of tokens in String array
*
* @param source
* , input String
* @param delimiter
* , delimiter to split on
* @return String[] , tokens in and array
*/
public static String[] splitString(String source, String delimiter) {
return source.split(delimiter);
}
}