Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
Posts: 227
Registered: ‎09-03-2010
My Device: Not Specified

HttpConnection

Hello,

 

How it would be possible to read the answer of a direction URL http://127.0.0.1/app/info.php example, using HttpConnection.

 

Thanks

Developer
Posts: 202
Registered: ‎04-19-2011
My Device: Blackberry Bold 9780
My Carrier: TAM

Re: HttpConnection

I'm suggest to

Don't use localhost ip for httpconnection...

Try using computer name exchange for localhost IP

Developer
Posts: 29
Registered: ‎05-23-2011
My Device: Curve
My Carrier: 8520

Re: HttpConnection

[ Edited ]

I dont know what u mean exactly with the word "answer" but u can get the full source of the page and then do a filter which searchs what u want

 

private String getViaHttpConnection(String url) throws IOException {
	String content = "";	

	try 
	{  
		StreamConnection s = null;

		s = (StreamConnection)Connector.open(url + getConnectionString());
		HttpConnection httpConn = (HttpConnection)s;                        


		int status = httpConn.getResponseCode();

		if (status == HttpConnection.HTTP_OK)
		{
			InputStream input = s.openInputStream();

			byte[] data = new byte[256];
			int len = 0;
			int size = 0;
			StringBuffer raw = new StringBuffer();

			while ( -1 != (len = input.read(data)) )
			{
				raw.append(new String(data, 0, len));
				size += len;    
			}   

			content = raw.toString();
			input.close();                      
		} 
		else 
		{                            
			content = "response code = " + status;
		}  
		s.close();                    
	} 
	catch (final IOCancelledException e) 
	{                              
		return "response code = " + status;
	}
	catch (final IOException e) 
	{  
		return "response code = " + status;
	}
	
	return content;
}

private static String getConnectionString() {
	String connectionString = null; 

	// Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR variable.
	if(DeviceInfo.isSimulator())
	{
		connectionString = ";deviceside=true";
	}

	// Is the carrier network the only way to connect?
	else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
	{

		String carrierUid = getCarrierBIBSUid();
		if(carrierUid == null)
		{
			// Has carrier coverage, but not BIBS. So use the carrier's TCP network
			connectionString = ";deviceside=true";
		}
		else
		{
			// some other method			
		}
	} 

	// Check for an MDS connection instead (BlackBerry Enterprise Server)
	else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
	{
		connectionString = ";deviceside=false";
	}
	
	// If there is no connection available abort to avoid bugging the user unnecssarily.
	else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
	{
		connectionString = "";
	}

	// Wifi is the not-preferred transmission method
	else if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
	{
		connectionString = ";interface=wifi";
	}
	
	// In theory, all bases are covered so this shouldn't be reachable.
	else
	{
		connectionString = ";deviceside=true";
	} 

	return connectionString;
}

 

a String content = getViaHttpConnection("http://your_External_or_Internal_IP/app/info.php");

will give u the page content in total including for example "<DOCTYPE ...."

 

now you can cut the String to receive what u want.

 

In total i think its a little easier to build a Service which gives a response in XML-format which is a way easier and better to parse with your cell.