2014-05-10 61 views
0

我在另一个方法MyVimeo()中调用方法GetVal()。Codename1 - 未分配给变量的值

变量时间戳,随机数,符号是全局的,并打印在方法GetVal()中。 但是,当在MyVimeo()方法中打印相同值时,这些值为空。

是否有任何替代方法返回值作为字符串从GetVal()?

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

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

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

+0

修改代码addToQueueAndWait解决了它。谢谢。 – vasanthkumar