In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to generate signature strings according to the class in Android. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Public String Signstr (T t) {
String str = ""
/ / define a list of attribute names
List lstfieldname = new ArrayList ()
/ / get all the attributes in the current class
Field [] fields = t.getClass () .getFields
/ / iterate through all attributes and write attribute names to List
For (Field field: fields) {
/ / the judgment is to write the attribute name defined by ourselves.
If (! field.isSynthetic ()) {
Lstfieldname.add (field.getName ())
}
}
/ / sort the ASCII codes of Lst.
Collections.sort (lstfieldname)
/ / We begin to concatenate strings according to the sorted name
For (String fieldname: lstfieldname) {
/ / get attribute value
String fieldvalue = ""
Try {
Fieldvalue = (String) t.getClass () .getField (fieldname) .get (t)
} catch (Exception e) {
E.printStackTrace ()
}
Str = str + fieldname + "=" + fieldvalue + "&"
}
/ / remove the last & symbol of the string
Str = str.substring (0, str.length ()-1)
Return str
}
From the above code, we can see that it is much more troublesome than the C# we wrote yesterday. The main reason is that Linq is really easy to use.
The implementation steps in the code:
Define the List that generates the attribute name string
Get an array of all the attribute fields of the incoming generic class, and then insert them into our List. Note: when we traverse the fields, we must judge field.isSynthetic () = false, otherwise some properties included in the system, such as this,0, will be listed.
Sort ASCII by List attribute name (Collections.Sort ())
Traverse our List attribute name and find the corresponding attribute value according to the attribute name to concatenate the string
Remove the splicing character of the last bit.
Output string
Code writing
Here we don't create a new Android project and test it on a page of a program I'm working on right now. There is a textView on the page
Load the textView in the code, and then write the event of textView.
From the above, we can see that we have also built a user information class for User, and a Sign is the class we use to generate signatures.
User user information class
Four fields are listed, and we assign them directly in the constructor, so we don't write any more code.
Sign generates signature class
Results of operation
Click on the business Fragment to display the new results.
After reading the above, do you have any further understanding of how to generate signature strings based on classes in Android? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.