Pobieranie strony z Callable

0

Witam, chciałem za pomoca wielowatkowosci pobrać źródło adresu url. Adres przesyłam w konstruktorze klasy. Callable zawsze zwraca wartosc w tym przypadku chce by zwracał String. Co robie nie tak? Jako wynik zwraca mi null?

@Override
	public String call() throws Exception {
		
		
		String html;
		URL myUrl = new URL(x);
        BufferedReader in = new BufferedReader(new InputStreamReader(myUrl.openStream()));

        while ((html = in.readLine()) != null) {
            html += html;

            }
        return html;
		
		
	}
ExecutorService service = Executors.newSingleThreadExecutor();
		 Future<String> future1 = service.submit(new site("http://wp.pl")); 
		 
		 String data1 = future1.get();
		 System.out.println(" Wynik to " + data1);
0

A wiesz jaki stan ma zmienna html przed zwróceniem ? pewnie jest nullem

0

zakładam że jest w niej zawrtość strony

2
viamarimar napisał(a):

zakładam że jest w niej zawrtość strony

hope is not a design tool - sprawdź, nie zakładaj

       String html; <---- TU JEST NULL
        URL myUrl = new URL(x);
        BufferedReader in = new BufferedReader(new InputStreamReader(myUrl.openStream()));
 
        while ((html = in.readLine()) != null) { <--- TU NIE WCHODZISZ 
            html += html;
 
            }
        return html;  <-- TU JEST NULL BARDZIEJ OCZYWISTY NIZ TO ZE AGH TO SLABA UCZELNIA
1

Za każdym razem nadpisujesz wartość zmiennej HTML linią z readera, a na koniec nadpisujesz nullem...

0

aj głupota dzieki!

1 użytkowników online, w tym zalogowanych: 0, gości: 1