0

我想将一个字符串从公共空间中取出并放入我的 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);



    }

}
4

4 回答 4

1

试试这个 :

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



    }

}
于 2012-04-18T10:16:18.630 回答
0

只需将 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");
}
于 2012-04-18T10:14:21.007 回答
0

我想你想这样做...

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

    }

}
于 2012-04-18T10:15:33.533 回答
0

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

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

}}
于 2012-04-18T10:25:50.550 回答