I made a method that retrieves certain data from an Ebay search. Here is the code that I am using:
public static LinkedList<EbaySearchResult> getResults(String search){
LinkedList<EbaySearchResult> results = new LinkedList<EbaySearchResult>();
LinkedList<String> links = new LinkedList<String>();
LinkedList<String> titles = new LinkedList<String>();
LinkedList<String> prices = new LinkedList<String>();
LinkedList<String> images = new LinkedList<String>();
try{
Scanner reader = new Scanner(new URL(String.format("http://www.ebay.com/sch/i.html?_trksid=p5197.m570.l1313&_nkw=%s&_sacat=0", search)).openStream());
while(reader.hasNextLine()){
String line = reader.nextLine().trim();
if(line != null && !line.equals("")){
if(line.contains(CONTAINS_ONE) && line.contains(CONTAINS_TWO)){
String[] splitter = line.split(CONTAINS_SPLIT);
String[] splitter2 = splitter[1].split(CONTAINS_SPLIT_2);
images.add(splitter2[0].trim());
}
if(line.contains(INFO_CONTAINS)){
String[] splitter = line.split(INFO_SPLIT);
String[] splitter2 = splitter[1].split(INFO_SPLIT_2);
String[] splitter3 = splitter[1].split(INFO_SPLIT_3);
String[] splitter4 = splitter3[1].split(INFO_SPLIT_4);
links.add(splitter2[0].trim());
titles.add(splitter4[0].trim());
}
if(line.equalsIgnoreCase(PRICE_INFO)){
String next = reader.nextLine().trim();
String[] splitter = next.split(PRICE_SPLIT);
String[] splitter2 = splitter[1].split(PRICE_SPLIT_2);
prices.add(splitter2[0].trim());
}
if(line.contains(PRICE_EXTENDED_INFO)){
String[] splitter = line.split(PRICE_SPLIT);
String[] splitter2 = splitter[1].split(PRICE_SPLIT_2);
prices.add(splitter2[0].trim());
}
}
}
if(!links.isEmpty()){
try{
for(int i = 0; i < prices.size(); i++){
if(titles.get(i) != null && prices.get(i) != null && links.get(i) != null && images.get(i) != null){
final String title = titles.get(i);
final String price = prices.get(i);
final String link = links.get(i);
final String image = images.get(i);
results.add(new EbaySearchResult(title, price, link, image));
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}catch(Exception e){
e.printStackTrace();
}
return results;
}
The problem is is that my internet keeps dropping for, literally, a split second. I’ve tried searching other questions on this site for “how to check if you’re still connected”, or something similiar. I’ve tried multiple methods but none of the methods seem to work for me. The problem is is that when my internet drops for that split second, the whole method seems to break, and becomes useless (it seems that the Scanner does not automatically recover). What I want to do is add in a check to see if I’m still connected to the site, and if I’m not, I basically want to loop back to the beginning (I know how to do the looping part, but I just need the method that checks for whether or not I am connected) Any help would be greatly appreciated.
NOTE: There are no stack traces.
EDIT: I know that it is still running and the method is stalling because when I look at my console, the program is still running and basically all the code that is inside my main method, is actually calling the method ‘getResults’.
You could just put a loop around your try/catch block and terminate when you want to stop reading from Ebay. If you get disconnected, the stream should throw an exception when you try to read from it, the exception gets caught, and at this point (maybe in a
finally{}) make sure your resources are freed, then it loops back to the top of thetry{}where a new Scanner is created and a new connection is made.By making sure the resources are freed, you probably want an InputStream declaration outside of the try block, and set it to the
Url.openStream()call so that you can callclose()on the input stream in afinally{}block.