简体   繁体   English

java 代码的简单行正在捕获 UnsupportedEncodingException,我不知道为什么

[英]Simple line of java code is catching a UnsupportedEncodingException and I can't figure out why

I am completely stuck on this one.我完全坚持这个。 I know my code is correct.我知道我的代码是正确的。 It is very simple.这很简单。 I have tried comparing what to null , using .equals , and now .length() , but it keeps catching this exception.我尝试使用.equals和现在的.length()将什么与null进行比较,但它一直在捕获这个异常。 Does anyone know what is going on?有谁知道发生了什么?

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]  

http://download.oracle.com/javase/1.4.2/docs/api/java/net/URLEncoder.html#encode%28java.lang.String,%20java.lang.String%29 http://download.oracle.com/javase/1.4.2/docs/api/java/net/URLEncoder.html#encode%28java.lang.String,%20java.lang.String%29

public static String encode(String s, String enc) throws UnsupportedEncodingException公共 static 字符串编码(字符串 s,字符串编码)抛出 UnsupportedEncodingException

The methode "URLENCODER.encode(what, "UTF-8") is the line, where you get the exception.方法 "URLENCODER.encode(what, "UTF-8") 是你得到异常的那一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM