hello
hello

Reputation: 1228

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.

Upvotes: 1

Views: 3711

Answers (2)

ASH TAMIZHA
ASH TAMIZHA

Reputation: 1

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

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();
    }

Upvotes: 0

user207421
user207421

Reputation: 310883

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.

Upvotes: 5

Related Questions