hello hello - 23 days ago 10
Android Question

Cannot resolve method openConnection()

I'm trying to download web content from a URL using the code below:

URL content = new URL(urlstr);
HttpURLConnection urlConnection = (HttpURLConnection) content.openConnection();

try {
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);

String existing_string = "";
StringBuilder builder = new StringBuilder(existing_string);
int data = isw.read();
while (data != -1) {
char current = (char) data;
builder.append(current);
data = isw.read();
}


The error I get is
Cannot resolve method openConnection()
. Import statements look like this:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;


I have look at ALMOST all SO related questions on this, but I dont seem to get it working.

EJP EJP
Answer

You must have a class of your own called URL, possibly this one. Don't do that. Don't use existing JDK class names for your own.

Comments