forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPubnubUtil.java
More file actions
61 lines (57 loc) · 1.44 KB
/
PubnubUtil.java
File metadata and controls
61 lines (57 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.pubnub.api;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* PubnubUtil class provides utility methods like urlEncode etc
* @author Pubnub
*
*/
public class PubnubUtil extends PubnubUtilCore {
/**
* 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;
}
}