简体   繁体   中英

Android passing a variable from a public void to onCreate

im wanting to get a String out of a public void and into my onCreate so i can Log it to check the hash is working. but i'm not sure how to do this

heres the my code

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);



    }

}

try this :

 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);



    }

}

or :

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);



    }

}

Just put the Log line into the hash() method and call it from onCreate:

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

        hash();

}

public void hash(){
   Log.v("myApp", "HelloWorld");
}

i think you want to do this...

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){
       }

    }

}

what i got from your question try using this way

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);

}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM