Search code examples
javaapirestxml-rpcsubtitle

OpenSubtitles API Response


I can communicate but I expect to get a list of subtitles in the Object. Here is my code:

    public static void makerequest(){


        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    XMLRPCClient client = new XMLRPCClient(new URL("https://api.opensubtitles.org/xml-rpc"));
                    HashMap ed = (HashMap<Object,String>) client.call("LogIn",username,password,"en",useragent);
                    String Token = (String) ed.get("token");
                    Map<String, String> videoProperties = new HashMap<>();
                    videoProperties.put("sublanguageid", "en");
                    videoProperties.put("imdbid", "528809");
                    Object[] videoParams = {videoProperties};
                    Object[] params = {Token, videoParams};


                    HashMap test2 = (HashMap<Object,String>) client.call("SearchSubtitles",params);
                    Object[] d = (Object[]) test2.get("data");

                    Log.d("diditworkstring", String.valueOf(d));
                } catch (Exception ex) {
                // Any other exception
                    Log.d("diditworkexception", String.valueOf(ex));
                }
            }

        };
        thread.start();
    }

In my log I get the following:

Log: {seconds=0.188, data=[Ljava.lang.Object;@2ec1b40, status=200 OK}

I thought I would see a list of subtitle information. I see that in this response (data=Ljava.Object;@23c1b40). is there something in that Object??


Solution

  • Below is the code that ultimately worked. I don't know the proper terminology but here is my best shot at explaining what I was doing wrong. I was trying to directly look at the Object as a string. After viewing it with Arrays.asList() I was able to see the data. Then each item in the list I cast as Map. After that I was able to get/change anything my heart desired.

    Hope this Helps someone some day :)

            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        // Setup XMLRPC Client
                        XMLRPCClient client = new XMLRPCClient(new URL("https://api.opensubtitles.org/xml-rpc"));
                        HashMap ed = (HashMap<Object,String>) client.call("LogIn",username,password,"en",useragent);
                        // separate my Token from the reply
                        String Token = (String) ed.get("token");
                        // setup Parameters for next call to search for subs
                        Map<String, String> videoProperties = new HashMap<>();
                        videoProperties.put("sublanguageid", "en");
                        videoProperties.put("query", "blade 2");
                        Object[] videoParams = {videoProperties};
                        Object[] params = {Token, videoParams};
                        // Make next call include method and Parameters
                        java.util.HashMap test2 = (HashMap<String,Array>) client.call("SearchSubtitles",params);
                        // select data key from test2
                        Object[] d = (Object[]) test2.get("data");
                        // change d Object to List
                        List ee = Arrays.asList(d);
                        // Grab Map from list
                        Map xx = (Map) ee.get(1);
                        Log.d("diditworkstring", String.valueOf(xx.get("ZipDownloadLink")));
                    } catch (Exception ex) {
                    // Any other exception
                        Log.d("diditworkexception", String.valueOf(ex));
                    }
                }
    
            };