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
Contributor
dpolonsky
Posts: 27
Registered: ‎10-02-2010
My Device: Not Specified

Re: Useful Custom Functions for all !!!

[ Edited ]

Hi Guys

In replace and replace all functions if there is no matching pattern the returned string is empty

source you are returning empty string, while it should return the original string.

 

 

		if ( sb.length() == 0)
		{
			return source;
		}
		else
		{
			return sb.toString();
		}

 

 

Developer
KushBB
Posts: 374
Registered: ‎04-07-2010
My Device: Bold 9000

Re: Useful Custom Functions for all !!!

Thanks for the correction dpolonsky...

Press the Like button (Thumb up) on the left side to thank and
please mark the Thread as solved if its solved...
K.S
Developer
KushBB
Posts: 374
Registered: ‎04-07-2010
My Device: Bold 9000

Re: Useful Custom Functions for all !!!

Create a Populated Text File in Device Memory:

 

 

public void createFile(){

public String filepath;
public FileConnection folder = null;
public FileConnection file = null;

try
		{
			String path = "file:///store/home/user/mine/";
			String filename = "Sample.txt";
			if (path == null)
			{
				throw new Exception(" Unable to get path...Please Check it...");

			}
			filepath = path+filename;

			System.out.println(filepath);

		        folder = (FileConnection) Connector.open("file:///store/home/user/mine/");
			file = (FileConnection) Connector.open(filepath, Connector.READ_WRITE);

			if (!folder.exists())
			{
				folder.mkdir();
			}

			if (!file.exists())
			{
				file.create();
				outStream = file.openOutputStream();

				String config = "Data in File";
				System.out.println(config);
				byte[] real = config.getBytes();

				outStream.write(real);
				outStream.close();
				file.close();
				System.out.println("Entry Saved...");
			}
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if (outStream != null)
			{
				try
				{
					outStream.close();
				}
				catch (IOException e)
				{
				}
			}
			if (folder != null)
			{
				try
				{
					folder.close();
				}
				catch (IOException e)
				{
				}
			}
			if (file != null)
			{
				try
				{
					file.close();
				}
				catch (IOException e)
				{
				}
			}

		}
          }

 

 

Press the Like button (Thumb up) on the left side to thank and
please mark the Thread as solved if its solved...
K.S
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Useful Custom Functions for all !!!

[ Edited ]

Download and Show Image on the Screen.

   
public EncodedImage startImage()
{
HttpConnection hc=null;
DataInputStream dis=null;
try
{
hc=(HttpConnection)Connector.open(Imageurl);
hc.setRequestMethod(HttpConnection.GET);
int st=hc.getResponseCode();
int len=(int)hc.getLength();
System.out.println("Code==>"+hc.getResponseCode());
System.out.println("Message==>"+hc.getResponseMessage());
byte bts[]=new byte[len];
dis=new DataInputStream(hc.openInputStream());
dis.readFully(bts);
image=EncodedImage.createEncodedImage(bts,0,len);
dis.close();
hc.close();
}catch(Exception e){}
return image;
}

//to Show on the screen
EncodedImage image1=startImage();
BitmapField bimage=new BitmapField();
bimage.setImage(image1);
add(bimage);

 ------------------------------------------------------------------------------------
Press Kudo to say thank to developer.
Also Press the Accept as solution Button when u got the Solution.

 

Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Useful Custom Functions for all !!!

[ Edited ]

//Accessing .Net WebService

 

package source;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import net.rim.device.api.crypto.certificate.x509.*;
import java.util.Date;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.i18n.DateFormat;

class SampleScreen extends MainScreen implements FieldChangeListener
{
ButtonField bf,bf1;
RichTextField txt=new RichTextField();
//Screen _screen;
String localService = "http://microsoft.com/webservices/";
String serviceurl = "";
String serviceMethod = "getDetails";
String serviceNamespace = "http://litwinconsulting.com/webservices/";
String soapAction = "http://tempuri.org/getDetails";

public SampleScreen()
{
bf=new ButtonField("Click");
bf.setChangeListener(this);
add(bf);
// _screen.add(new RichTextField("POP SCREEN"));
}

public void webservice()
{
add(new RichTextField(":Start Process:"));

String[] vct;
String [] output = null;
String tst;

HttpTransport ht = null;
try
{
ht = new HttpTransport(serviceurl);
ht.debug = true;
SoapObject request = new SoapObject(localService, serviceMethod);
request.addProperty("SINo", "");
request.addProperty("ProductId", "");
request.addProperty("productname", "");
request.addProperty("price", "");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;

//Add Mapping
//addClassMappings(envelope);

ht.call(soapAction, envelope);
add(new RichTextField("Process......."));
if ( envelope.getResponse() != null )
{
SoapObject resp = (SoapObject)envelope.getResponse();
int count = resp.getPropertyCount();
output = new String[count];

output[0] = resp.getProperty(1).toString();
}
add(new RichTextField(""+output[0]));

//UiApplication.getUiApplication().popScreen(_screen);
}catch(Exception ex){System.out.println(""+ex);}
}

public void fieldChanged(Field f,int Context)
{
if(f==bf)
{
webservice();
bf1=new ButtonField("update");
bf1.setChangeListener(this);
add(bf1);

}
if(f==bf1)
{
UiApplication.getUiApplication().pushScreen(new updateData());
}
}
}

------------------------------------------------------------------------------------
Press Kudo to say thank to developer.
Also Press the Accept as solution Button when u got the Solution.

Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Useful Custom Functions for all !!!

Hey KushBB, Great Efforts. This is very nice Thread for Everyone.

I Appriciate it.

 

Thanks.

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Useful Custom Functions for all !!!

i cannot recommend to use nitinverma274 sample, it executes networking on the event thread which will eventually kill the application.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
abhay_401
Posts: 185
Registered: ‎09-02-2010
My Device: Not Specified

Re: Useful Custom Functions for all !!!

Hi friends,

 

                       It's nice ,it should be helpful for everyone

Developer
KushBB
Posts: 374
Registered: ‎04-07-2010
My Device: Bold 9000

Re: Useful Custom Functions for all !!!

Thanks Simon,

 

I see you have pointed same thing in an another place on these forums...

what I get is: The networking should be called in a seperate thread which itself is called in invokeLater block...

Tell me if I am right or wrong about it....

Press the Like button (Thumb up) on the left side to thank and
please mark the Thread as solved if its solved...
K.S
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Useful Custom Functions for all !!!

place networking into a separate thread (and call this thread with start() ).

you can use invokelater (or synchronize on the event lock) to execute UI updates from without the event thread, for example when you are inside the separated thread you just started.

 

 please find more information here, for example:

http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

 

another excellent source are the developer guides:

http://docs.blackberry.com/en/developers/subcategories/?userType=21&category=Development+Guides

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter