Java Android Studio:错误:非法字符:'\u2028'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34578142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio:error: illegal character: '\u2028'
提问by jublikon
I am trying to do a JSONObject request:
我正在尝试执行 JSONObject 请求:
final String URL = "https://some/url";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
params.put("param1", param1);
?params.put("param2", param2);
?params.put("param3", param3);?
params.put("param4", param4);
JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response:%n %s", "l?uft");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
});
// add the request object to the queue to be executed
NetworkController.getInstance().addToRequestQueue(req);
I cannot compile the project because I get a syntax error for the params:
我无法编译项目,因为参数出现语法错误:
Error:(144, 9) error: illegal character: '\u2028'
错误:(144, 9) 错误:非法字符:'\u2028'
How can I fix that?
我该如何解决?
采纳答案by jublikon
Well, just deleting all the characters and rewriting them again helped. So crazy..
好吧,只是删除所有字符并再次重写它们会有所帮助。如此疯狂..
回答by Prateek Jassal
I faced the exact same issue but in my case the number of errors were pretty large(95 or so). The only sane thing to do was to replace these characters with an empty character. This short Python script would do that and print out the new contents for your file. Run it with the correct path to your file. Cheers.
我遇到了完全相同的问题,但在我的情况下,错误数量非常大(95 左右)。唯一明智的做法是用空字符替换这些字符。这个简短的 Python 脚本将执行此操作并打印出文件的新内容。使用正确的文件路径运行它。干杯。
with open(‘filename.java', ‘r') as file:
data=(file.read().decode(“utf-8”)).replace(u'\u2028', ‘').encode(“utf-8”)
print data
回答by JoelWass
It's the new line character, if you go to each of the lines that are causing the error and delete the 'invisible' last character then the errors will resolve
这是换行符,如果您转到导致错误的每一行并删除“不可见”的最后一个字符,则错误将解决
Go to end of the line that is causing the error and hit backspace once, for each of the lines that have the illegal character error.
转到导致错误的行的末尾,并为具有非法字符错误的每一行按一次退格键。
回答by Ronaldo Albertini
Cut and paste the code into a text editor to convert it to simple text. Then remove all extra spaces. After that, use Android Studio's Reformat codefeature to make it nice again.
将代码剪切并粘贴到文本编辑器中以将其转换为简单文本。然后删除所有多余的空格。之后,使用 Android Studio 的Reformat code功能使其再次变得漂亮。
For me, only deleting the spaces in Android Studio did not work.
对我来说,只删除 Android Studio 中的空格是行不通的。
回答by Chirag Purohit
If you are mac user then you can
如果您是 mac 用户,那么您可以
Copy and paste text in TextWrangler View -> Text? Display -> Show Invisibles
在 TextWrangler 视图中复制和粘贴文本 -> 文本?显示 -> 显示隐形
It will show you symbol like "|". Delete this and you are good to go.
它会显示像“|”这样的符号。删除它,你就可以开始了。