2015-09-27 50 views
0

我在android studio中使用JSoup。在我的代码,我有这样的:JSoup从String到Int

Elements description = document.select("something"); 

如果我这样做 -

String foo = description.text(); 

一切工作正常。

但如果我这样做 -

int y = Integer.parseInt(description.text()); 

为什么我的应用程序崩溃?

我的所有代码:

public Void doInBackground(Void... params) { 
     try { 
      // Connect to the web site 
      Document document = Jsoup 
        .connect("something") 
        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko") 
        .get(); 
      Elements description = document.select("something"); 

      String foo = description.text(); 
      int fuu = Integer.parseInt(foo); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
+2

发布错误。 –

+2

wild guess:description.text()不是有效整数 – Reimeus

+0

Try:Integer.parseInt(description.text()。toString()); –

回答

0

这是几乎不可能说出它是什么在你的代码事情没有堆栈跟踪和原始的HTML或URL。但是,我发现崩溃的一种可能性,那就是该字符串不包含有效的数字。既然你声明字符串的打印输出有效地打印出“65”,我只能假定该字符串可能使用奇怪的uft8 unicode字符,它们看起来像或是数字,但不能被Integer.parseInt(String)解释。

例如,如果输入字符串为 “” 它可能看起来像 “65”,但它是使用U + 1D7FC(数学MONOSPACE DIGIT SIX)和U + 1D7FB(数学MONOSPACE DIGIT FIVE)

的unicode在这样的Unicode字符数列表,可以发现here

一个解决(suggested in the comment here)可能会使用Normalizer class

String numberStr = Normalizer.normalize(description.text(), Form.NFKC); 
Integer.parseInt(numberStr); 
0

请尽量int foo = Integer.parse(description.ownText());

相关问题