Posnazim sa to vysvetlit:
Metoda1 [Object], ktora podla parametra ktorym je link stranky, stiahne cely zdrojovy kod stranky do buffru, vyselektuje nejake hodnoty a vrati ich.
Metoda2 [void], ktora prepisuje neaktualne hodnoty v ArrayListe na najnovsie hodnoty z netu (ktore dostane z Metody1). Kedze linky sa nachadzaju v ArrayListe (ktore potrebuje Metoda1 ako parameter), vyuziva sa foreach cyklus.
Ide o to, ze ak spustim program, nejake hodnoty sa vyselektovat podari, no potom nastane ten dany citovany problem. Vacsinou (takych 70% pripadov) sa uspesne podari spracovat prve 3 linky (3/7) z ArrayListu. Niekedy 4, niekedy 6 a RAZ sa mi podarilo vsetky. Neviem kde je problem...skusal som DEBUG a krok po kroku som to presiel, logicku chybu som nenasiel a v debugy presiel cely program so 100% uspesnostou...Najvacsi bonus na tom vsetkom je neziaduci vypadok internetu sposobeny mojou aplikaciou
Myslel som si, ze je to sposobene rychlostou ciklu for ktory mohol sposobovat nedaj boze DDOS utok. Preto som vlozil do cyklu kod: "Thread.sleep(10000);" vysledok bol taky, ze na spracovanie 3. linku som cakal 30 sekund a 4. link nespracovalo..ten isty prob. Par krat sa mi podarilo spracovat 5 alebo 6 linkov stale inak...no vacsinou 3, niekedy 1 niekedy vobec ziaden...
Metoda1[kod]:
Kód:
public Object[] getSite(String link) {
Object[] nameAndPrice = {null, null};
URL url = null;
URLConnection urlConn = null;
InputStreamReader inStream = null;
BufferedReader buffer = null;
String nextLine;
try {
url = new URL(link);
urlConn = url.openConnection();
inStream = new InputStreamReader(
urlConn.getInputStream());
buffer = new BufferedReader(inStream);
while (true) {
nextLine = buffer.readLine();
.
.
SELEKTOVANIE STRANKY (tu chyba nie je, cyklus bude brake-nuty)
.
.
}
nameAndPrice[0] = finalName;
nameAndPrice[1] = finalPriceWithoutTax;
return nameAndPrice;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Metoda2[kod]:premennda products bola deklarovana takto: List<Product> products = new ArrayList<Products>();Kód:
public void valueUpdate() {
for (Product product : products) {
Object[] o = getSite(product.getLink());
System.out.println(o[0].toString() + " " + o[1].toString());
product.setName((String) o[0]);
product.setPriceWithoutTax((Double) o[1]);
}
}
Chybovy vypis:
Citácia:
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:837)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:778)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:703)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1026)
at pricelist.MainFrame.getSite(MainFrame.java:340)
at pricelist.Main.main(Main.java:14)
Exception in thread "main" java.lang.NullPointerException
at pricelist.Main.main(Main.java:15)
Java Result: 1
Cely den nad tym sedim, no neviem prist na to, co to moze sposobovat...
ma niekto nejake rady? budem velmi vdacny...(topiaci sa aj slamky chyta)