我完全被这个问题困住了。我知道我的代码是正确的。这很简单。我尝试使用 .equals
与 null
进行比较,现在使用 .length()
,但它不断捕获此异常。有谁知道这是怎么回事吗?
public static void findLocations(String what, String near, String near2, String type) {
StringBuilder responseBuilder = new StringBuilder();
try {
URL url;
String tempURL = "http://.........";
if (what.length() != 0) { // This line is catching UnsupportedEncodingException
tempURL += "&category=" + URLEncoder.encode(what, "UTF-8");
}
if (near.length() != 0) {
Thread [<1> main] (Suspended)
LocationSet.findLocations(String, String, String, String) line: 85
SearchAlgorithm.editNear() line: 322
SearchAlgorithm.search(String, String) line: 90
SearchView.onClick(View) line: 281
Button(View).performClick() line: 2408
View$PerformClick.run() line: 8816
ViewRoot(Handler).handleCallback(Message) line: 587
ViewRoot(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
最佳答案
public static String encode(String s, String enc) throws UnsupportedEncodingException
方法“URLENCODER.encode(what, "UTF-8") 是在其中出现异常的行。
关于java - 简单的java代码行捕获了UnsupportedEncodingException,我不明白为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6793511/