I want to get the source code of a web page that the users enters. When he presses the button, he should see the source in a TextView. This is my code:
final Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
URL url = null;
url = new URL(myEditText.getText().toString());
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
myTextView.append(line);
}
} catch (Exception e) {
Log.e("ERR",e.getMessage());
}
}
});
When I run it I get a NullPointerException at
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
.–>Println needs a message.
I don’t know what is wrong, since this is from a video tutorial. I have written <uses-permission android:name="android.permission.INTERNET"/> in Manifest so, everything should be allright.
try this.
if it works then you need to set validation for like isValidUrl() ?
Because user may have entered wrong URL.