我正在做一个简单的应用程序,它从中获取数据,editText
然后获取该文本并发送到 java 类(在类中我需要它将字符串转换为 utf8 编码)。我应该怎么做才能从活动中发送数据以及如何在 java 类中接收它?
这是我的 UTFencoder 类:
public class UTFencoder {
public void main(String[] args) {
String testString = encodeStringToUTF8("this is my app ... eskadenia");
System.out.println(testString);
}
public static String encodeStringToUTF8(String stringToEncode) {
String newValue = "";
List<String> charactersCode = new ArrayList<String>();
try {
byte[] stringBytes = stringToEncode.getBytes("UTF-8");
for (int i = 0; i < stringBytes.length; i++) {
//System.out.println("utf value is " + stringBytes[i]);
String value = String.valueOf(stringBytes[i]);
int no = Integer.parseInt(value);
String hex = Integer.toHexString(no);
//System.out.println("Hex value is " + hex);
for (int j = 0; j < hex.length(); j++) {
charactersCode.add(String.valueOf(hex.charAt(j)));
}
}
} catch (UnsupportedEncodingException e) {
System.out.println("Cannot encode " + stringToEncode
+ " to UTF-8, UnsupportedEncodingException");
}
Iterator iterator =charactersCode.iterator();
while(iterator.hasNext()){
Object element= iterator.next();
System.out.print(element+" ");
}
return newValue;
}
}