Codename1 - 未分配给变量的值

时间:2014-05-10 08:21:47

标签: codenameone

在另一个方法MyVimeo()。

中调用方法GetVal()

变量timestamp,nonce,sign是全局的并且在方法GetVal()中打印。 但是,当在MyVimeo()方法中打印相同值时,这些值为空。

是否有任何替代方法可以将GetVal()中的值作为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();
}

1 个答案:

答案 0 :(得分:1)

addToQueue是异步的,所以它已经在一个单独的线程上工作,你不需要从另一个线程调用它(你真的不应该这样做)。

您可以使用addToQueueAndWait,但最好在获取数据后调用其余的处理代码。