本文整理汇总了Java中org.json.JSONArray.optString方法的典型用法代码示例。如果您正苦于以下问题:Java JSONArray.optString方法的具体用法?Java JSONArray.optString怎么用?Java JSONArray.optString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.json.JSONArray
的用法示例。
在下文中一共展示了JSONArray.optString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: showNotification
import org.json.JSONArray; //导入方法依赖的package包/类
private void showNotification(JSONArray arguments, CallbackContext context) {
Context acontext = TwilioVoicePlugin.this.webView.getContext();
NotificationManager mNotifyMgr =
(NotificationManager) acontext.getSystemService(Activity.NOTIFICATION_SERVICE);
mNotifyMgr.cancelAll();
mCurrentNotificationText = arguments.optString(0);
PackageManager pm = acontext.getPackageManager();
Intent notificationIntent = pm.getLaunchIntentForPackage(acontext.getPackageName());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("notificationTag", "BVNotification");
PendingIntent pendingIntent = PendingIntent.getActivity(acontext, 0, notificationIntent, 0);
int notification_icon = acontext.getResources().getIdentifier("notification", "drawable", acontext.getPackageName());
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(acontext)
.setSmallIcon(notification_icon)
.setContentTitle("Incoming Call")
.setContentText(mCurrentNotificationText)
.setContentIntent(pendingIntent);
mNotifyMgr.notify(mCurrentNotificationId, mBuilder.build());
context.success();
}
示例2: getChecksFromJson
import org.json.JSONArray; //导入方法依赖的package包/类
/**
* Gets a list of connection checks from JSON. If json does not contain a 'check' field
* null will be returned instead.
*
* @param json The JSON to extract the connection checks from.
* @param checksKey The key for the checks.
*
* @return The set of checks or null.
*/
@Nullable
public static String[] getChecksFromJson(JSONObject json, String checksKey) {
Object checkObj = json.opt(checksKey);
String[] checks = null;
if (checkObj == null) {
// Do nothing, ignore other checks
} else if (checkObj instanceof JSONArray) {
JSONArray jsonChecks = (JSONArray) checkObj;
if (jsonChecks != null) {
int count = jsonChecks.length();
checks = new String[count];
for (int i = 0; i < count; i++) {
checks[i] = jsonChecks.optString(i);
if (checks[i] == null) {
throw new IllegalArgumentException("Malformatted check array in Input.");
}
}
}
} else if (checkObj instanceof String) {
checks = new String[]{(String) checkObj};
}
return checks;
}
示例3: parseVersionSpec
import org.json.JSONArray; //导入方法依赖的package包/类
private static int[] parseVersionSpec(JSONArray versionsJSON) {
// Null signifies no overrides to the min-version as specified by the SDK.
// An empty array would basically turn off the dialog (i.e no supported versions), so
// DON'T default to that.
int[] versionSpec = null;
if (versionsJSON != null) {
int numVersions = versionsJSON.length();
versionSpec = new int[numVersions];
for (int i = 0; i < numVersions; i++) {
// See if the version was stored directly as an Integer
int version = versionsJSON.optInt(i, NativeProtocol.NO_PROTOCOL_AVAILABLE);
if (version == NativeProtocol.NO_PROTOCOL_AVAILABLE) {
// If not, then see if it was stored as a string that can be parsed out.
// If even that fails, then we will leave it as NO_PROTOCOL_AVAILABLE
String versionString = versionsJSON.optString(i);
if (!isNullOrEmpty(versionString)) {
try {
version = Integer.parseInt(versionString);
} catch (NumberFormatException nfe) {
logd(LOG_TAG, nfe);
version = NativeProtocol.NO_PROTOCOL_AVAILABLE;
}
}
}
versionSpec[i] = version;
}
}
return versionSpec;
}
示例4: stringToStringArray
import org.json.JSONArray; //导入方法依赖的package包/类
public static ArrayList<String> stringToStringArray(String s) {
ArrayList<String> strings = new ArrayList<String>();
if (TextUtils.isEmpty(s) == false) {
try {
JSONArray a = new JSONArray(s);
for (int i = 0; i < a.length(); i++) {
String url = a.optString(i);
strings.add(url);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return strings;
}
示例5: parseStructure
import org.json.JSONArray; //导入方法依赖的package包/类
private void parseStructure() {
components = new HashMap<>();
final JSONArray names = runtime.names();
for (int i = 0; i < names.length(); i++) {
final String current = names.optString(i);
if (current == null) {
continue;
}
switch (current) {
case NAVIGATION:
navigation = new Navigation(runtime.optJSONObject(current), formattedAssetUrl);
break;
case KOLIBRI_VERSION:
version = runtime.optString(current);
break;
case DOMAIN:
domain = runtime.optString(current);
break;
case SCHEME:
scheme = runtime.optString(current);
break;
case STYLING:
styling = new Styling(runtime.optJSONObject(current));
default:
components.put(current, new Component(runtime.optJSONObject(current)));
break;
}
}
}
示例6: parseJSONArray
import org.json.JSONArray; //导入方法依赖的package包/类
/**
* 解析JSONArray 获取数组 例如:["1","2","3","4"]
*
* @param jsonArray 数据源
* @param array 返回值
* @return
*/
public static String[] parseJSONArray(JSONArray jsonArray, String[] array) {
if (jsonArray != null) {
try {
array = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
array[i] = jsonArray.optString(i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return array;
}
示例7: setTags
import org.json.JSONArray; //导入方法依赖的package包/类
public void setTags(JSONArray tags) {
if (tags != null) {
int length = tags.length();
String[] newTags = new String[length];
for (int i = 0; i < length; i++) {
newTags[i] = tags.optString(i);
}
setTags(newTags);
}
}
示例8: getError
import org.json.JSONArray; //导入方法依赖的package包/类
@Override
public String getError(String key) {
if (errors == null)
return "";
JSONArray items = errors.optJSONArray(key);
if (items == null)
return "";
return items.optString(0);
}
示例9: getArgument
import org.json.JSONArray; //导入方法依赖的package包/类
/**
* Convenience method to read a parameter from the list of JSON args.
* @param args the args passed to the Plugin
* @param position the position to retrieve the arg from
* @param defaultString the default to be used if the arg does not exist
* @return String with the retrieved value
*/
private static String getArgument(JSONArray args, int position, String defaultString) {
String arg = defaultString;
if (args.length() > position) {
arg = args.optString(position);
if (arg == null || "null".equals(arg)) {
arg = defaultString;
}
}
return arg;
}
示例10: routeScreenOrientation
import org.json.JSONArray; //导入方法依赖的package包/类
private boolean routeScreenOrientation(JSONArray args, CallbackContext callbackContext) {
String action = args.optString(0);
String orientation = args.optString(1);
Log.d(TAG, "Requested ScreenOrientation: " + orientation);
Activity activity = cordova.getActivity();
if (orientation.equals(ANY)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
} else if (orientation.equals(LANDSCAPE_PRIMARY)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if (orientation.equals(PORTRAIT_PRIMARY)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (orientation.equals(LANDSCAPE)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else if (orientation.equals(PORTRAIT)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
} else if (orientation.equals(LANDSCAPE_SECONDARY)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
} else if (orientation.equals(PORTRAIT_SECONDARY)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
callbackContext.success();
return true;
}
示例11: recoveryFromRecord
import org.json.JSONArray; //导入方法依赖的package包/类
private int recoveryFromRecord() {
if (this.config.recorder == null) {
return 0;
}
byte[] data = this.config.recorder.get(this.recorderKey);
if (data == null) {
return 0;
}
try {
JSONObject obj = new JSONObject(new String(data));
int offset = obj.optInt("offset", 0);
long modify = obj.optLong("modify_time", 0);
int fSize = obj.optInt("size", 0);
JSONArray array = obj.optJSONArray("contexts");
if (offset == 0 || modify != this.modifyTime || fSize != this.size || array == null
|| array.length() == 0) {
return 0;
}
for (int i = 0; i < array.length(); i++) {
this.contexts[i] = array.optString(i);
}
return offset;
} catch (JSONException e) {
e.printStackTrace();
return 0;
}
}
示例12: initLogger
import org.json.JSONArray; //导入方法依赖的package包/类
private void initLogger(final JSONArray args, final CallbackContext callbackContext) {
try {
final String jsFileName = args.optString(0) != null ?
args.optString(0) : DEFAULT_JS_FILENAME;
final String lcFileName = args.optString(1) != null ?
args.optString(1) : DEFAULT_LC_FILENAME;
this.maxFileSizeInKB = args.optString(2) != null ?
args.optInt(2) : DEFAULT_MAX_FILESIZE_IN_KB;
this.filterBy = args.optString(3) == null ?
null : args.optString(3).split(ARRAY_SEPARATOR);
this.filterOut = args.optString(4) == null ?
null : args.optString(4).split(ARRAY_SEPARATOR);
this.enableCallback = args.optBoolean(5);
if (jsFileName != null) {
this.jsFile = new File(this.internalStorage, jsFileName);
this.jsBak = FileTools.rollFile(this.jsFile, null, LOG_ROLLING_EXTENSION);
this.jsCon = FileTools.rollFile(this.jsFile, LOG_CON_SUFFIX);
if (this.jsFileWriter == null) {
this.jsFileWriter = new JsFileWriter(this.jsFile, this.jsBak,
this.cordovaInstance, this.maxFileSizeInKB);
}
}
if (lcFileName != null) {
this.lcFile = new File(this.internalStorage, lcFileName);
this.lcBak = FileTools.rollFile(this.lcFile, null, LOG_ROLLING_EXTENSION);
this.lcCon = FileTools.rollFile(this.lcFile, LOG_CON_SUFFIX);
}
this.zipFile = new File(this.internalStorage, DEFAULT_ZIP_FILENAME);
callbackContext.success();
} catch (Exception e) {
callbackContext.error(e.getMessage());
}
}
示例13: call
import org.json.JSONArray; //导入方法依赖的package包/类
public String call(WebView webView, String jsonStr) {
if (!TextUtils.isEmpty(jsonStr)) {
try {
JSONObject callJson = new JSONObject(jsonStr);
String methodName = callJson.getString("method");
JSONArray argsTypes = callJson.getJSONArray("types");
JSONArray argsVals = callJson.getJSONArray("args");
String sign = methodName;
int len = argsTypes.length();
Object[] values = new Object[len + 1];
int numIndex = 0;
String currType;
values[0] = webView;
for (int k = 0; k < len; k++) {
currType = argsTypes.optString(k);
if ("string".equals(currType)) {
sign += "_S";
values[k + 1] = argsVals.isNull(k) ? null : argsVals.getString(k);
} else if ("number".equals(currType)) {
sign += "_N";
numIndex = numIndex * 10 + k + 1;
} else if ("boolean".equals(currType)) {
sign += "_B";
values[k + 1] = argsVals.getBoolean(k);
} else if ("object".equals(currType)) {
sign += "_O";
values[k + 1] = argsVals.isNull(k) ? null : argsVals.getJSONObject(k);
} else if ("function".equals(currType)) {
sign += "_F";
values[k + 1] = new JsCallback(webView, mInjectedName, argsVals.getInt(k));
} else {
sign += "_P";
}
}
Method currMethod = mMethodsMap.get(sign);
// 方法匹配失败
if (currMethod == null) {
return getReturn(jsonStr, 500, "not found method(" + sign + ") with valid parameters");
}
// 数字类型细分匹配
if (numIndex > 0) {
Class[] methodTypes = currMethod.getParameterTypes();
int currIndex;
Class currCls;
while (numIndex > 0) {
currIndex = numIndex - numIndex / 10 * 10;
currCls = methodTypes[currIndex];
if (currCls == int.class) {
values[currIndex] = argsVals.getInt(currIndex - 1);
} else if (currCls == long.class) {
// WARN: argsJson.getLong(k + defValue) will return
// a bigger incorrect number
values[currIndex] = Long.parseLong(argsVals.getString(currIndex - 1));
} else {
values[currIndex] = argsVals.getDouble(currIndex - 1);
}
numIndex /= 10;
}
}
return getReturn(jsonStr, 200, currMethod.invoke(null, values));
} catch (Exception e) {
// 优先返回详细的错误信息
if (e.getCause() != null) {
return getReturn(jsonStr, 500, "method execute error:" + e.getCause().getMessage());
}
return getReturn(jsonStr, 500, "method execute error:" + e.getMessage());
}
} else {
return getReturn(jsonStr, 500, "call data empty");
}
}
示例14: call
import org.json.JSONArray; //导入方法依赖的package包/类
public String call(WebView webView, JSONObject jsonObject) {
long time = 0;
if (LogUtils.isDebug()) {
time = android.os.SystemClock.uptimeMillis();
}
if (jsonObject != null) {
try {
String methodName = jsonObject.getString(KEY_METHOD);
JSONArray argsTypes = jsonObject.getJSONArray(KEY_TYPES);
JSONArray argsVals = jsonObject.getJSONArray(KEY_ARGS);
String sign = methodName;
int len = argsTypes.length();
Object[] values = new Object[len];
int numIndex = 0;
String currType;
for (int k = 0; k < len; k++) {
currType = argsTypes.optString(k);
if ("string".equals(currType)) {
sign += "_S";
values[k] = argsVals.isNull(k) ? null : argsVals.getString(k);
} else if ("number".equals(currType)) {
sign += "_N";
numIndex = numIndex * 10 + k + 1;
} else if ("boolean".equals(currType)) {
sign += "_B";
values[k] = argsVals.getBoolean(k);
} else if ("object".equals(currType)) {
sign += "_O";
values[k] = argsVals.isNull(k) ? null : argsVals.getJSONObject(k);
} else if ("function".equals(currType)) {
sign += "_F";
values[k] = new JsCallback(webView, mInterfacedName, argsVals.getInt(k));
} else {
sign += "_P";
}
}
Method currMethod = mMethodsMap.get(sign);
// 方法匹配失败
if (currMethod == null) {
return getReturn(jsonObject, 500, "not found method(" + sign + ") with valid parameters", time);
}
// 数字类型细分匹配
if (numIndex > 0) {
Class[] methodTypes = currMethod.getParameterTypes();
int currIndex;
Class currCls;
while (numIndex > 0) {
currIndex = numIndex - numIndex / 10 * 10 - 1;
currCls = methodTypes[currIndex];
if (currCls == int.class) {
values[currIndex] = argsVals.getInt(currIndex);
} else if (currCls == long.class) {
//WARN: argsJson.getLong(k + defValue) will return a bigger incorrect number
values[currIndex] = Long.parseLong(argsVals.getString(currIndex));
} else {
values[currIndex] = argsVals.getDouble(currIndex);
}
numIndex /= 10;
}
}
return getReturn(jsonObject, 200, currMethod.invoke(mInterfaceObj, values), time);
} catch (Exception e) {
LogUtils.safeCheckCrash(TAG, "call", e);
//优先返回详细的错误信息
if (e.getCause() != null) {
return getReturn(jsonObject, 500, "method execute error:" + e.getCause().getMessage(), time);
}
return getReturn(jsonObject, 500, "method execute error:" + e.getMessage(), time);
}
} else {
return getReturn(jsonObject, 500, "call data empty", time);
}
}
示例15: getString
import org.json.JSONArray; //导入方法依赖的package包/类
protected String getString(JSONArray array, int index) {
return array.optString(index, "");
}