I am trying to get Information from a website and display it in an Android app. The second answer to the question "What is the fastest way to scrape HTML webpage in Android?" suggested to use BufferedReader. In the answer, the person uses the URL class. I tried to implement the answer like this:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
TextView display = (TextView) findViewById(R.id.textDisplay);
@Override
protected void onCreate(Bundle savedInstanceState) throws Exception {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
URL url = new URL("http://stackoverflow.com/questions/2971155");
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
for (String line; (line = reader.readLine()) != null; ) {
builder.append(line.trim());
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException logOrIgnore) {
logOrIgnore.printStackTrace();
}
}
}
String start = "<div class=\"post-text\"><p>";
String end = "</p>";
String part = builder.substring(builder.indexOf(start) + start.length());
String question = part.substring(0, part.indexOf(end));
TextView display = (TextView) findViewById(R.id.textDisplay);
display.setText(question);
}
}
I got this error:
'onCreate(Bundle)' in 'com.example.myproject.MainActivity' clashes with
'onCreate(Bundle)' in 'android.appcompat.app.AppCompatActivity';
overridden method does not throw 'java.lang.Exception'
What would you suggest to handle this, is this a smart way of getting data from a website? Any help is much appreciated