简体   繁体   中英

How do I remove the “null” word when I append characters to a String?

I am trying to print the HTML page source of a website. I initialized the String as null in line 45. However, when I try to print the newly appended string the null keyword is displayed.

I tried removing the initialization of String.

 public class MainActivity extends AppCompatActivity {
  public class ToPrintWebSiteSource extends AsyncTask<String,Void,String>{
    @Override
        HttpURLConnection httpURLConnection = null;
        String result = null;
        try {
            siteUrl = new URL(urls[0]);
            httpURLConnection = (HttpURLConnection) siteUrl.openConnection();
            InputStream in = httpURLConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();
            while(data!= -1){
                char character = (char) data;
                result += character;
                data = reader.read();
            }
        }
        catch (Exception e) {
            Log.i("Error:","The code is not working....");
            e.printStackTrace();
        }
        return  result;

    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String result = null;
    ToPrintWebSiteSource helloWorld = new ToPrintWebSiteSource();
    try {
        result = helloWorld.execute("https://web.ics.purdue.edu/~gchopra/class/public/pages/webdesign/05_simple.html").get();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Log.i("Page Source Html:", result);
    }
   }

The result I received was:

null<html>

<head>
<title>A very simple webpage</title>
<basefont size=4>
</head>

You should set it to an empty string and not null. When you concatenate a string with null , the string "null" gets added to it, rather than being empty.

Better yet, you shouldn't even be using string concatenation here. This is what a StringBuilder is for.

    HttpURLConnection httpURLConnection = null;
    StringBuilder result = new StringBuilder();
    char buffer[] = new char[100];

    try {
        siteUrl = new URL(urls[0]);
        httpURLConnection = (HttpURLConnection) siteUrl.openConnection();
        InputStream in = httpURLConnection.getInputStream();
        InputStreamReader reader = new InputStreamReader(in);

        for (int n; (n = reader.read(buffer)) != -1;) {
            result.append(buffer, 0, n);
        }
    }
    catch (Exception e) {
        Log.i("Error:","The code is not working....");
        e.printStackTrace();
    }
    return result.toString();

This reads multiple chars at a time into a char array, appending the characters to the StringBuilder until all the data is read. Setting the size of the array to 100 was arbitrary, you can make it larger if you want to read more data at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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