How to generate signature string according to class sort in Android
Android how to sort according to the class to generate signature strings, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
First, let's take a look at the class we built.
There are only two properties, merid and appid.
And then the SignStr function.
This is what we said in the previous article, "[practical information] Android generates signature strings based on classes" to the method, so I won't go into details here.
Let's run a trace and take a look.
Enter our characters into the program and run the trace
See that there is an extra serialVersionUID=, in the blue flag in the red box above and enter this function to see the following figure. There is an extra attribute in the fields obtained in it.
When we know the reason, we will revise the plan.
We are adding two judgments, serialVersionUID and change, to solve this problem.
Core code
Public class SignHelper {
Public static 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 ()) {
If (! (field.getName () .equals ("change") | | field.getName () .equals ("serialVersionUID")) {
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
}
}
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.