[android]应用程序不抓HTML

发布时间: 2017/3/12 23:04:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

编辑︰

错误︰

06-12 19:25:55.880: ERROR/AndroidRuntime(226): Uncaught handler: thread main exiting due to uncaught exception
06-12 19:25:55.910: ERROR/AndroidRuntime(226): java.lang.NullPointerException
06-12 19:25:55.910: ERROR/AndroidRuntime(226):     at com.laytproducts.songmaster.mainAct$1.onClick(mainAct.java:124)
06-12 19:25:55.910: ERROR/AndroidRuntime(226):     at android.view.View.performClick(View.java:2364)
06-12 19:25:55.910: ERROR/AndroidRuntime(226):     at android.view.View.onTouchEvent(View.java:4179)
06-12 19:25:55.910: ERROR/AndroidRuntime(226):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
06-12 19:25:55.910: ERROR/AndroidRuntime(226):     at android.view.View.dispatchTouchEvent(View.java:3709)
06-12 19:25:55.910: ERROR/AndroidRuntime(226):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-12 19:25:55.910: ERROR/AndroidRuntime(226):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-12 19:25:55.910: ERROR/AndroidRuntime(226):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-12 19:25:55.910: ERROR/AndroidRuntime(226):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
....

代码︰

rawHtml = getHtml(baseSite + rSearched); //get the raw html of page
if(rawHtml == null || rawHtml.length() < 1){//checks if it really contains anything
    Toast.makeText(getApplicationContext(), "Error: Got No Result", Toast.LENGTH_SHORT).show();
    Log.e("RawHtml Error:1", "Nothing In rawHtml String");
} else {
    for(int i = 1; i < 7; i++){
        String html = parseHtml(rawHtml,i);
        if(html == null || html.length() < 1){
            results[i-1] = "Result not found:Please try different lyrics";
        } else {
            results[i-1] = parseHtml(rawHtml,i); //error here
        }
    }
}

parseHtml:

public String parseHtml(String html,int num){
    String parsed = "";
    String artistParse = "";
    String songParse = "";
    //String fullHtmlParse = "#NUMBER#. &nbsp;<span>This Charming Man</span> &nbsp; by Smiths</a>";//Reference
    if(num != 0 && num <= 6){
        songParse = StringUtils.substringBetween(html,num+". &nbsp;<span>","</span>");
        artistParse = StringUtils.substringBetween(html,num+". &nbsp;<span>"+songParse+"</span> &nbsp; by ","</a>");
    } else {
        Toast.makeText(getApplicationContext(),
                "Error: Number is wrong in parseHtml, Please Try Again.", Toast.LENGTH_SHORT).show();
        Log.e("ParsedHtml Error:1","Error: Number in parseHtml is invalid: " + num);
        return "";
    }
    parsed = songParse + ":" + artistParse;
    return parsed;
}

getHtml:

public String getHtml(String url){
    String html = "";
    String baseHtml = "";
    String table = "";
    try {
        baseHtml = new StringReader(url).toString();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Error getting HtmlDoc, Please Try Again.", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
        return "";
    }
    if(baseHtml == null || baseHtml.length() < 1){
        Toast.makeText(getApplicationContext(), "Error getting HtmlDoc, Please Try Again.", Toast.LENGTH_SHORT).show();
        Log.e("BaseHtml Error:1","Error: Nothing in baseHtml[method getHtml(String url)]");
        return "";
    } else {
        //table = StringUtils.substringBetween(baseHtml,"<!-- EyesLyrics.com search results -->","</table>");
    }
    html = baseHtml;
    return html;
}

希望这是你的需要。

解决方法 1:

使 Toast 出现,你会需要调用 show() -方法。喜欢这个:

Toast.makeText(getApplicationContext(), "Error getting HtmlDoc, Please Try Again.", Toast.LENGTH_SHORT).show();

看到这里,更多的细节。


if(rawHtml == "" || rawHtml == null){[...]}

我猜 rawHtml -对象是一个字符串?在这种情况下,如果你想要检查这个字符串是否为空,你将既不使用 equals("") -方法或检查字符串的长度︰

if (rawHtml.length < 1)

此外,如果您需要检查是否字符串是 null ,你应该第一,因为对长度的检查 (例如) 会导致 NullPointerException


关于您的代码

if(rawHtml == null ...

测试是否 rawHtml 是 null 是不必要的因为在 getHtml 的方法,你创造了它作为一个空字符串。它将永远不会是 null

return "";

你返回一个空字符串在你 parseHtmlgetHtml 的方法。我宁可会返回 null ,然后检查返回的值是 null 。你能匀出同一个条件 effect

results[i-1] = "...";

计算应在方括号中,像这样做︰

results[(i-1)] = "...";

要在这条线你错误 aperas:

results[i-1] = parseHtml(rawHtml,i);

正如我不能看到是否你初始化 results -对象,我想这是你的问题。你可以访问之前 (写或读) element 在此数组中,数组需要将其初始化。

这是怎么你会这样做,猜测您的数组一个字符串数组︰

String[] results = new String[NumberOfElements];

我希望这能够解决你的问题。

官方微信
官方QQ群
31647020