【问题标题】:Android passing a variable from a public void to onCreateAndroid 将变量从公共 void 传递给 onCreate
【发布时间】:2012-04-29 17:43:18
【问题描述】:

我想从公共空白中获取一个字符串并进入我的 onCreate,以便我可以记录它以检查哈希是否有效。但我不知道该怎么做

这是我的代码

public class ChooseTeamActivity extends ListActivity {

    private static final String apiKey = "4545ggg454hfnf7557kfdkgg454"; 
    private static final String apiUser = "AndroidUser"; 

    long unixTimeStamp = System.currentTimeMillis() / 1000L;

    String newFeedRequest = "1.0/evoStructure?timestamp=" + unixTimeStamp;
    String fixturesFeedURL = "https://secure.TestSite.com/_services/api/" + newFeedRequest;

    public void hash() throws NoSuchAlgorithmException, UnsupportedEncodingException{

        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(fixturesFeedURL.getBytes("UTF-8"));
        byte[] digest = md.digest();
        String strhash = new String(digest);

    }   



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

    setContentView(R.layout.chooseact);


     Log.v("myApp", fixturesFeedURL);
     Log.v("myApp", strhash);



    }

}

【问题讨论】:

  • 在你的 onCreate 中调用这个 hash() 方法。

标签: java android public void oncreate


【解决方案1】:

我从你的问题中得到的尝试使用这种方式

private static final String apiKey = "4545ggg454hfnf7557kfdkgg454"; 
private static final String apiUser = "AndroidUser"; 
String strhash="";
long unixTimeStamp = System.currentTimeMillis() / 1000L;

String newFeedRequest = "1.0/evoStructure?timestamp=" + unixTimeStamp;
String fixturesFeedURL = "https://secure.TestSite.com/_services/api/" + newFeedRequest;

public void hash() throws NoSuchAlgorithmException, UnsupportedEncodingException{

    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(fixturesFeedURL.getBytes("UTF-8"));
    byte[] digest = md.digest();
    strhash = new String(digest);

}   



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

setContentView(R.layout.chooseact);


 Log.v("myApp", fixturesFeedURL);
 Log.v("myApp", strhash);

}}

【讨论】:

    【解决方案2】:

    试试这个:

     public class ChooseTeamActivity extends ListActivity {
    
        private static final String apiKey = "4545ggg454hfnf7557kfdkgg454"; 
        private static final String apiUser = "AndroidUser"; 
    
        long unixTimeStamp = System.currentTimeMillis() / 1000L;
    
        String newFeedRequest = "1.0/evoStructure?timestamp=" + unixTimeStamp;
        String fixturesFeedURL = "https://secure.TestSite.com/_services/api/" + newFeedRequest;
        String strHash = null;
        public void hash() throws NoSuchAlgorithmException, UnsupportedEncodingException{
    
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(fixturesFeedURL.getBytes("UTF-8"));
            byte[] digest = md.digest();
            strHash = new String(digest);
    
        }   
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
    
        setContentView(R.layout.chooseact);
    
         hash();
         Log.v("myApp", fixturesFeedURL);
         Log.v("myApp", strhash);
    
    
    
        }
    
    }
    

    或:

    public class ChooseTeamActivity extends ListActivity {
    
        private static final String apiKey = "4545ggg454hfnf7557kfdkgg454"; 
        private static final String apiUser = "AndroidUser"; 
    
        long unixTimeStamp = System.currentTimeMillis() / 1000L;
    
        String newFeedRequest = "1.0/evoStructure?timestamp=" + unixTimeStamp;
        String fixturesFeedURL = "https://secure.TestSite.com/_services/api/" + newFeedRequest;
    
        public String hash() throws NoSuchAlgorithmException, UnsupportedEncodingException{
    
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(fixturesFeedURL.getBytes("UTF-8"));
            byte[] digest = md.digest();
            return new String(digest);
    
        }   
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
    
        setContentView(R.layout.chooseact);
    
         String strhash = hash();
         Log.v("myApp", fixturesFeedURL);
         Log.v("myApp", strhash);
    
    
    
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      我想你想这样做......

      public class ChooseTeamActivity extends ListActivity {
      
          private static final String apiKey = "4545ggg454hfnf7557kfdkgg454"; 
          private static final String apiUser = "AndroidUser"; 
      
          long unixTimeStamp = System.currentTimeMillis() / 1000L;
      
          String newFeedRequest = "1.0/evoStructure?timestamp=" + unixTimeStamp;
          String fixturesFeedURL = "https://secure.TestSite.com/_services/api/" + newFeedRequest;
      
          public void hash() throws NoSuchAlgorithmException, UnsupportedEncodingException{
      
              MessageDigest md = MessageDigest.getInstance("SHA-256");
              md.update(fixturesFeedURL.getBytes("UTF-8"));
              byte[] digest = md.digest();
              String strhash = new String(digest);
      
           Log.v("myApp", fixturesFeedURL);
           Log.v("myApp", strhash);
          }   
      
      
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
      
          setContentView(R.layout.chooseact);
          try{ 
           hash();
            }catch(Exception e){
             }
      
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        只需将 Log 行放入 hash() 方法并从 onCreate 中调用:

        @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
        
                hash();
        
        }
        
        public void hash(){
           Log.v("myApp", "HelloWorld");
        }
        

        【讨论】:

          猜你喜欢
          • 2017-06-30
          • 2023-03-23
          • 2015-01-09
          • 1970-01-01
          • 2012-03-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多