开发者

App Not Grabbing HTML

Edit:

Error:

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)
....

code:

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>";//R开发者_如何学Pythoneference
    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;
}

Hope that is what you need.


To make the Toast appear, you'll need to call the show()-method. Like this:

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

See here for more details.


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

I guess the rawHtml-object is a String? In that case, if you want to check if this String is empty, you would neither use the equals("")-method or check for the String's length:

if (rawHtml.length < 1)

Also, if you need to check if the String is null, you should do that first, because the check for the length (for example) would cause a NullPointerException.


About your code

if(rawHtml == null ...

Testing if rawHtml is null is unnecessary, because in the getHtml-method, you crate it as an empty String. It will never be null.

return "";

You return an empty String in your parseHtml and getHtml-methods. I would rather return null and then check if the returned value is null. You can spare one condition with the same effect.

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

The calculation should be done in brackets, like this:

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

Your error aperas to be on this line:

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

As I can't see if you initialize the results-object, I guess this is your problem. Before you can access (write or read) an element in this array, the array needs to be initialized.

This is how you would do this, guessing your array is a String-array:

String[] results = new String[NumberOfElements];

I hope this solves your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜