hello hello - 1 year ago 152
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;
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download