如何转换使用命令行参数的java类在android中使用?

时间:2012-02-22 08:02:30

标签: java android command-line-arguments

我遇到了使用命令行参数的java类的问题,我必须在我的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> 

2 个答案:

答案 0 :(得分:5)

一般来说,您必须创建一个用户可以输入所有值的UI。

然后添加一个按钮,将这些值转换为String数组并调用main()

更干净的解决方案是将字符串复制到新的&#34; config&#34;对象并让按钮填写相同的配置对象。然后,您可以编写一个以配置对象作为参数的方法。

答案 1 :(得分:0)

这不是更好的方法,但我认为以下将是一个简单快捷的解决方案:

首先,为main函数指定一个合适的名称。然后使用varargs而不是String数组作为参数。此外,如果遇到无效参数(例如IllegalArgumentException),则抛出适当类型的异常。

因此,将main重写为

public static void listVideos(String ... args) throws IllegalArgumentException

另外,将System.exit(1)替换为

throw new IllegalArgumentException("Invalid arguments!");

现在,您可以将代码重用为:

VideoListExample.listVideos("-oatoken", "<token>", 
                            "-oasecret", "<secret>", "<username>");