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
Highlighted
New Contributor
Posts: 8
Registered: ‎10-11-2011
My Device: 9700
My Carrier: Maxis
Accepted Solution

http connection on real device works on wifi but for wap/tcp sometimes give 404 response

Hi, im a new blackberry developer, i have  no problem when using the connection using wifi but when i off my wifi and try to connect using 3g sometimes the data is send but sometimes it dont...what could possibly error in this case?

try {
	

String _postData =;
				OutputStream os;

				//the preferred network is already arranged here
				int[] preferredTransportTypes = {
						TransportInfo.TRANSPORT_TCP_WIFI,
						TransportInfo.TRANSPORT_WAP2,
						TransportInfo.TRANSPORT_TCP_CELLULAR};
				ConnectionFactory factory = new ConnectionFactory();
				factory.setPreferredTransportTypes(preferredTransportTypes);
				factory.setAttemptsLimit(3);
	   	            	factory.setConnectionTimeout(10000);
	            		//factory.setTimeLimit(10000);
				//modify this
       final ConnectionDescriptor connDescriptor = factory.getConnection("http://url here");
	            if(connDescriptor==null){
					UiApplication.getUiApplication().invokeLater(new Runnable()
					{
						public void run()
						{
							Dialog.alert("Invalid Network. Please check your network set up.");
						}
					});
				}else{
					HttpConnection http = (HttpConnection) connDescriptor.getConnection();					
					//https.setRequestMethod(HttpsConnection.POST);
					//byte[] postDataBytes = _postData.getBytes();
					http.setRequestMethod(HttpConnection.POST);
					http.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
					http.setRequestProperty("Content-Language", "en-US");
					http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
					//https.setRequestProperty("Content-length", Integer.toString(_postData.length()));
					byte [] postDataBytes = _postData.getBytes("UTF-8");
					
					os = http.openOutputStream();
					os.write(postDataBytes);
					os.flush();
					os.close();
					int state = http.getResponseCode();
					final int state2 = state;
					if(state==HttpConnection.HTTP_OK || state==HttpConnection.HTTP_BAD_GATEWAY){
						UiApplication.getUiApplication().invokeLater(new Runnable()
						{
							public void run()
							{   
								
								//original
								//Dialog.alert("SOS sent. Our customer care personnel will contact you shortly.");
								Dialog.alert("SOS sent. This is the response code : " + state2);
								//Dialog.inform("SOS sent. Our customer care personnel will contact you shortly.");
							}
						});
					} else {
						UiApplication.getUiApplication().invokeLater(new Runnable()
						{
							public void run()
							{                      
								//Dialog.alert("Failed to send SOS .");
								Dialog.alert("Failed to send SOS. Please try again.This is the response code : " + state2);
							}
						});
					}
				}
			} catch(IOException e) {Dialog.inform(e.getMessage());}

  im using blackberry 9700 with os 6

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: http connection on real device works on wifi but for wap/tcp sometimes give 404 response

[ Edited ]

Welcome to the forums.

 

Sorry ignore this post, I failed to see the comment about the 404 and answered a different question.  Sorry I see no reason why you would sometimes get a a 404 using WAP on the same fphone. 

New Contributor
Posts: 8
Registered: ‎10-11-2011
My Device: 9700
My Carrier: Maxis

Re: http connection on real device works on wifi but for wap/tcp sometimes give 404 response

hi peter

for api 5.0 we have ConnectionFactory which make things easier to make a http connection. Do we still need the additional parameter for the url? Do it have something to do with the server side?
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: http connection on real device works on wifi but for wap/tcp sometimes give 404 response

If you use ConnectionFactory, you don't need to code the 'connection string'.

New Contributor
Posts: 8
Registered: ‎10-11-2011
My Device: 9700
My Carrier: Maxis

Re: http connection on real device works on wifi but for wap/tcp sometimes give 404 response

im putting the code inside thread

 

private class ConnectionThread extends Thread {

		/**
		 * This method is where the thread retrieves the content from the page 
		 * whose url is associated with this thread.
		 * @see java.lang.Runnable#run()
		 */
		public void run()
		{
                   //my connection goes here
                }

 and calling it from the listener

 

int response = Dialog.ask(Dialog.D_YES_NO,"Are you sure you want to send SOS? \n",Dialog.YES);
	if(response == Dialog.YES)
	{
	final Thread _ConnectionThread = new ConnectionThread();
						   _ConnectionThread.start();
						
	}

 

is this the correct way?

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: http connection on real device works on wifi but for wap/tcp sometimes give 404 response

Yes, that all looks good. 

New Contributor
Posts: 8
Registered: ‎10-11-2011
My Device: 9700
My Carrier: Maxis

Re: http connection on real device works on wifi but for wap/tcp sometimes give 404 response

When im using browser to access the url that i need to post data it works and at the same time i try with my apps the respond code i got is failed to send "404". I think the blackberry browser using BIS connection and to use BIS we need to subscribe Alliance program.

my solution is to provide WIFI,WAP and TCP. whats the difference between WAP and TCP? as I know TCP need apn configuration...but how about WAP? how is possible that WAP will failed on connection since i already check the available connection before proceed the POST. the signal of 3g is full
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: http connection on real device works on wifi but for wap/tcp sometimes give 404 response

You are using connection factory, it should sort out the requirements for WAP.

 

If you type 'network' in the Search box you will find a number of videos and other tutorial type information about these sorts of things.  I recommend that you review these to understand the various connection methods. 

New Contributor
Posts: 8
Registered: ‎10-11-2011
My Device: 9700
My Carrier: Maxis

Re: http connection on real device works on wifi but for wap/tcp sometimes give 404 response

Solved due to carrier issue for WAP2

 

so i set the TransportInfo.TRANSPORT_TCP_CELLULAR to be on top of WAP and it solves my problem.

but i have to set my apn setting for the tcp connection