4 Replies - 2040 Views - Last Post: 21 December 2013 - 05:51 AM Rate Topic: -----

#1 Martin.B  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 13

HttpClient 4.3 stuck on execute

Posted 19 December 2013 - 10:10 AM

My program submits a form with no problems the first time round, but when it needs read a live page again to get the information needed it just hangs there with no errors. I've set a timeout for it to disconnect but I need to know how to fix the problem in the first place.

The problem is in the ScanMarketsBazaar() function.

market class
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class market {

	Document doc;
	HttpEntity market;
	HttpPost httpget;
	HttpClient httpclient;
	HttpResponse responseMarket;
	
	//Used for filtering
	ArrayList <String> ids = new ArrayList<String>();
	ArrayList <String> prices = new ArrayList<String>();

	String[] Ids_array;
	String[] prices_array;
	
	int buyPrice;
	@SuppressWarnings("deprecation")
	public market(String itemName,HttpClient httpclient) throws ClientProtocolException, IOException
	{
		
		this.httpclient = httpclient;
		buyPrice = 25000;
		System.out.println("1");

	}
	
	public void scanMarket(String itemName) throws ClientProtocolException, IOException
	{

		httpget = new HttpPost("http://www.torn.com/imarket.php?type=66&step=shop&submit=Go");
		responseMarket = httpclient.execute(httpget);
		getLowestPriceAndIdBazaar(EntityUtils.toString(responseMarket.getEntity()));
	}
	
	public void ScanMarketsBazaar() throws ClientProtocolException, IOException
	{
		System.out.println("6");
		httpget = new HttpPost("http://www.torn.com/imarket.php?type=66&step=shop&submit=Go");
		System.out.println("7");
                //------------------------------------------------Problem here ------------------------


		responseMarket = httpclient.execute(httpget);


                //----------------------------------------------------------------------------------------
		System.out.println("8");
		//System.out.println(EntityUtils.toString(responseMarket.getEntity()));
		getLowestPriceAndIdBazaar(EntityUtils.toString(responseMarket.getEntity()));
		
	}
	
	public boolean buyBazaarItem(String price,String id, String amountAvailable ) throws ClientProtocolException, IOException
	{

		if (Integer.parseInt(price) <= buyPrice)
		{
			HttpPost buyFromBMarket = new HttpPost(("http://www.torn.com/bazaar.php?step=buy2&itemID=66&userID="+id+"&confirm=1&price="+price).replace(" ", ""));
			ArrayList<NameValuePair>nvps = new ArrayList <NameValuePair>();
	        nvps.add(new BasicNameValuePair("amount", "1"));
	        nvps.add(new BasicNameValuePair("beforeval", price));
	        
	        buyFromBMarket.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
	        HttpResponse response2 = httpclient.execute( buyFromBMarket );
	        HttpEntity entity = response2.getEntity();
	        System.out.println("I've bought Morphine from the market for " + price);
	        //httpget.releaseConnection();httpclient.close();
	        return true;
	        //System.out.println("-----" + EntityUtils.toString(entity));
		}else{
			System.out.println("There are no items in your price range");
			//httpget.releaseConnection();
			return true;
		}
	}

public String getLowestPriceAndIdBazaar(String page) throws ClientProtocolException, IOException
	{
		System.out.println("9");
		doc = (Document) Jsoup.parse(page);
		Elements links = doc.select("a[href]");

		String [] links_sep = links.toString().split("<a"); 
		ArrayList<String> b_prices = new ArrayList<String>();
		ArrayList<String> b_ids = new ArrayList<String>();

		for (int i = 0; i < links_sep.length;i++)
		{
			if (links_sep[i].contains("bazaar.php?userID"))
			{
				b_ids.add(links_sep[i].replace("href=\"/bazaar.php?userID=", "").split("\"")[0]);
			}
		}
		
		Elements prices = doc.select("b");
		
		for (int i = 0; i < prices.size();i++)
		{
			if (prices.get(i).toString().contains("$")){
				b_prices.add(prices.get(i).toString().replace("<b>", "").toString().replace("</b>", "").replace(",","").replace("$", ""));
			}
		}
		
		Elements amount_doc =  doc.select("font");
		ArrayList <String> amount = new ArrayList<String>();
		for (int i = 0; i < amount_doc.size();i++)
		{
			if (amount_doc.get(i).toString().contains("("))
			amount.add(amount_doc.get(i).toString().replace("<font color=\"#999999\" size=\"1\">(", "").replace(" in stock)</font>", "").replace(",", ""));
		}
		
		buyBazaarItem(b_prices.get(0),b_ids.get(0),amount.get(0));
		return page;
		
	}





Login
		while (true)
		{
		try {
		    Thread.sleep(10000);
		    System.out.print("|");
	        mar = new market("Morphine",httpclient);
		    mar.ScanMarketsBazaar();
		    
			}catch(InterruptedException ex) {
		    	Thread.currentThread().interrupt();
			}
		}



Output:
|1
6
7
8
9
2
3
I've bought Morphine from the market for 20400
|1
6
7



Is This A Good Question/Topic? 0
  • +

Replies To: HttpClient 4.3 stuck on execute

#2 Martin.B  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 13

Re: HttpClient 4.3 stuck on execute

Posted 19 December 2013 - 03:22 PM

Posted Image

Got it.

	
while (true)
	{
	  Thread.sleep(10000);
	  mar.ScanMarketsBazaar();
	}



I needed to stop creating a new market class each time. I don't see how that could mess up the client. I even made several threads and destroyed the old Login and market classes and it didn't stop the error.
Was This Post Helpful? 0
  • +
  • -

#3 Martin.B  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 13

Re: HttpClient 4.3 stuck on execute

Posted 19 December 2013 - 04:13 PM

No wait.

It's was this:
	        HttpResponse response2 = httpclient.execute( buyFromBMarket );
	        HttpEntity entity = response2.getEntity();
	        EntityUtils.consume(entity);

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: HttpClient 4.3 stuck on execute

Posted 20 December 2013 - 03:17 PM

	    ArrayList <String> ids = new ArrayList<String>();
038	    ArrayList <String> prices = new ArrayList<String>();
039	 
040	    String[] Ids_array;
041	    String[] prices_array;


Regardless of the purpose, parallel arrays are pretty poor practice. Consider a more OO approach instead- Moving Away From Parallel Arrays. OO code is much cleaner and easier to manage because of the benefits of encapsulation. If you want to sort the data, for instance, based on price, it is easier to do so with a single array (or List) of objects rather than with parallel arrays.
Was This Post Helpful? 1
  • +
  • -

#5 Martin.B  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 13

Re: HttpClient 4.3 stuck on execute

Posted 21 December 2013 - 05:51 AM

View Postmacosxnerd101, on 20 December 2013 - 03:17 PM, said:

	    ArrayList <String> ids = new ArrayList<String>();
038	    ArrayList <String> prices = new ArrayList<String>();
039	 
040	    String[] Ids_array;
041	    String[] prices_array;


Regardless of the purpose, parallel arrays are pretty poor practice. Consider a more OO approach instead- Moving Away From Parallel Arrays. OO code is much cleaner and easier to manage because of the benefits of encapsulation. If you want to sort the data, for instance, based on price, it is easier to do so with a single array (or List) of objects rather than with parallel arrays.



Thanks for the advice I'll fix up my program now. If you see any other way i can improve it please feel free to link me to a few more articles.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1