Question

Am calling the method GetVal() in another method MyVimeo().

The variables timestamp, nonce, sign are global and printed inside the method GetVal(). However the values are blank when the same are printed in the MyVimeo() method.

Is there any alternative to return the values from GetVal() as String ?

public void GetVal(final String param) {

            NetworkManager networkManager = NetworkManager.getInstance();
            networkManager.start();
            networkManager.addErrorListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    NetworkEvent n = (NetworkEvent) evt;
                    n.getError().printStackTrace();
                }
            });

            ConnectionRequest request = new ConnectionRequest() {

                int chr;
                StringBuffer sb = new StringBuffer();
                public String response = "";

                public void readResponse(InputStream input)
                        throws IOException {
                    // do something with input stream
                    while ((chr = input.read()) != -1) {
                        sb.append((char) chr);
                        //System.out.println("reading...");
                    }
                    response = sb.toString();
                    res = response;
                    Log.p("param->" + param);
                    if(param=="timestamp") {
                        timestamp = res;
                        Log.p("Response->" + timestamp);//values printed inside method
                    }
                    else if(param=="nonce") {
                        nonce = res;
                        Log.p("Response->" + nonce);//values printed inside method
                    }
                    else if(param=="sign") {
                        sign = res;
                        Log.p("Response->" + sign);//values printed inside method
                    }
                }

                protected void handleException(Exception err) {
                    Dialog.show("Connection Err!!", "Are you connected to the internet? Check your connection",
                            "Ok", null);
                }
            };

            request.setUrl("http://127.0.0.1/getvalues.php?param="+param);
            request.setPost(false);
            networkManager.addToQueue(request);
}




public void MyVimeo(final String file) {
    new Thread(new Runnable() {
        public void run() {
            Log.p("File Name : " + file);

            String consumer_key = "";
            String consumer_secret = "";

            String vimeoAPIURL = "http://vimeo.com/api/rest/v2";
            String reqTokenEP = "http://vimeo.com/oauth/request_token";
            String AUTHORIZATION_URL = "http://vimeo.com/oauth/authorize?oauth_token=";
            String accTokenEP = "http://vimeo.com/oauth/access_token";
            String accToken = "";
            String accTokenPass = "";

            NetworkManager networkManager = NetworkManager.getInstance();
            networkManager.start();
            networkManager.addErrorListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    NetworkEvent n = (NetworkEvent) evt;
                    n.getError().printStackTrace();
                }
            });

            ConnectionRequest request = new ConnectionRequest() {

                int chr;
                StringBuffer sb = new StringBuffer();
                String response = "";

                protected void readResponse(InputStream input)
                        throws IOException {
                    // do something with input stream
                    while ((chr = input.read()) != -1) {
                        sb.append((char) chr);
                        // System.out.println("reading...");
                    }
                    response = sb.toString();
                    Log.p("Response->" + response);
                    if (response.equals("OK")) {
                        Dialog.show("Response", "Authenticated", "Ok", null);
                    } else {
                        Dialog.show("Response", "Failed", "Ok", null);
                    }
                }

                protected void handleException(Exception err) {
                    // do something with err
                    Dialog.show(
                            "Connection Err!!",
                            "Are you connected to the internet? Check your connection",
                            "Ok", null);
                }
            };

            //timestamp = GetVal("timestamp");
            //nonce = GetVal("nonce");
            //sign = GetVal("sign");
            GetVal("timestamp");//values printed inside method
            GetVal("nonce");//values printed inside method
            GetVal("sign");//values printed inside method

            Log.p("TS->" + timestamp);//no values here
            Log.p("NC->" + nonce);//no values here
            Log.p("SIG->" + sign);//no values here

            String url = vimeoAPIURL + "?format=xml"+
            "&method=vimeo.videos.upload.getQuota"+
            "&oauth_consumer_key="+ consumer_key +
            "&oauth_version=1.0"+
            "&oauth_signature_method=HMAC-SHA1"+
            "&oauth_timestamp="+timestamp+
            "&oauth_nonce="+nonce+
            "&oauth_token="+accToken+
            "&oauth_signature="+sign;

            request.setPost(false);
            request.setUrl(url);
            Log.p("vimeoAPIURL->" + url);
            networkManager.addToQueue(request);
        }
    }).start();
}
Was it helpful?

Solution

addToQueue is asynchronous so it already works on a separate thread and you don't need to call it from another thread (and you really shouldn't do that).

You can use addToQueueAndWait but you are better off just calling the rest of your processing code after the data is fetched.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top