我在使用命令行参数的 java 类时遇到问题,我必须在我的 Android 应用程序中使用此类。 我的问题是如何在 android 中传递参数或通过手动输入参数在 android 应用程序中使用此文件的任何其他方式?
这是我必须在 android 中使用的 JAVA 类:
public class VideoListExample
{
///////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
try {
ParameterHelper helper = new ParameterHelper(args);
args = helper.getRemainingArgs();
if(args.length > 2) {
printUsage();
System.exit(1);
return;
}
String titleSearch = null;
if(args.length == 2) {
titleSearch = args[1];
}
Vzaar vzaar = helper.createVzaar();
VideoListRequest request = new VideoListRequest(args[0]);
request.setPage(1);
request.setSize(true);
request.setCount(100);
request.setSortAscending(true);
request.setTitleSearch(titleSearch);
List<Video> videoList = null;
while(!(videoList = vzaar.getVideoList(request)).isEmpty())
{
for(Video video : videoList) {
System.out.println(
"ID=" + video.getId() +
", Created=" + video.getCreatedAt() +
", Size=" + video.getWidth() + "x" + video.getHeight() +
", Duration=" + video.getDuration() + "s" +
", Play Count=" + video.getPlayCount() +
", URL=" + video.getUrl() +
", Title=" + video.getTitle());
}
if(videoList.size() < 100) break;
request.setPage(request.getPage() + 1);
}
}
catch(VzaarException e) {
System.err.println("Error: " + e.getMessage());
System.exit(2);
}
catch(ArrayIndexOutOfBoundsException e) {
printUsage();
System.exit(1);
}
catch(NumberFormatException e) {
printUsage();
System.exit(1);
}
}
///////////////////////////////////////////////////////////////////////////
private static final void printUsage() {
System.out.println("Usage: vzaar-list " +
ParameterHelper.getCommonCommandLineArgs() +
" <username> [<title-search>]\n");
System.out.println("Params:");
System.out.println(" <username> " +
"The user name to fetch active videos for");
System.out.println(" <title-search> " +
"Return only videos with title containing given string");
System.out.println(ParameterHelper.getCommonCommandLineHelp());
}
///////////////////////////////////////////////////////////////////////////
}
参数应该以这种方式传递:
java_class [-debug] [-ssl3] [-transport hc3|hc4] [-url <url>] [-oatoken <token>] [-oasecret <secret>] <username> [<title-search>]
我只需传递这三个参数:
java_class [-oatoken <token>] [-oasecret <secret>] <username>
最佳答案
一般来说,您必须创建一个用户可以在其中输入所有值的 UI。
然后添加一个按钮,将这些值转换为 String
数组并调用 main()
更干净的解决方案是将字符串复制到新的“配置”对象中,并让按钮填写相同的配置对象。然后,您可以编写一个以配置对象作为参数的方法。
关于java - 如何将使用命令行参数的java类转换为在android中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9391072/