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
Developer
ashish117
Posts: 40
Registered: ‎07-20-2010
My Device: Not Specified

sending data from application to a web form

Hi,

 

I am trying to store data from my application to a mysql database on the web. To do this, I have created a php based web form which is able to transfer data to the mysql database and am trying to send the data from my app to the web form.

 

the web form is located at -

http://www.planetimpressions.com/contacts/register.php

 

the application code is -

 

    public void run(){
        try {
          
            HttpConnection connection = (HttpConnection)Connector.open(url);
            connection.setRequestMethod(method);
            
            int responseCode = connection.getResponseCode();
            if(responseCode!=HttpConnection.HTTP_OK){
                screen.requestFailed("failed to create connection: " + responseCode);
                connection.close();
            }
            
            if(method.equals("POST")){


                String data = "data to be posted";
                byte[] postData = data.getBytes();
                screen.requestFailed("converted data to bytes");
                
                connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
                OutputStream requestOutput = connection.openOutputStream();
                requestOutput.write(postData);
                requestOutput.close();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            screen.requestFailed("failed: " + e.toString());
        }

    }

 

I think I am able to create the http connection but I have no idea how to transfer data from the application to each of the input boxes in the form.

 

Can anyone please help ?

 

Thanks

Please use plain text.
Regular Contributor
Abhijit_RIM
Posts: 98
Registered: ‎08-31-2010
My Device: BB-9700
My Carrier: Airtel

Re: sending data from application to a web form

You can implement intoerfaces for HTTPCommunication class. The class where you want the response data back, need to implement those interfaces. You can actually pass the data object as a parameter to call back.

 

Once you get call back into the implemented method you can use SetText method of the text box to be filled with corresponding values.

 

Sorry if i misunderstood your questions.

 

Thanks

 

Regards

-Abhijit

Please use plain text.
Developer
ashish117
Posts: 40
Registered: ‎07-20-2010
My Device: Not Specified

Re: sending data from application to a web form

 

Hi Abhijit,

 

I am trying to send data to the form and not recieve data from the form.

 

Thanks

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: sending data from application to a web form

Apologies, I don't have the time to go into depth right now, here is a quick summary to get you going.

 

Your Web form is doing a post to the URL specified in the action.  The form will have a number of ids that it sets, the URl specified in the action will pick up the values of these ids.  So this is the process you heed to replicate.

 

You do this by writing the ids and data into the request.  Here is some sample code, which has the ids

userName, and

userPwd

 

Hope this makes sense and you can use it.  Do a search for POST in this forum for other samples.

 

HttpConnection connection = (HttpConnection)Connector.open(url);
connection.setRequestMethod(method);
//Above as before
URLEncodedPostData postData = new URLEncodedPostData("", false);
postData.append("userName", email);
postData.append("userPwd", pwd);
byte[] postDataBytes = postData.getBytes();
connection.setRequestProperty("Content-Length", (new Integer(postDataBytes.length)).toString());
OutputStream os = connection.openOutputStream();
os.write(postDataBytes);
os.flush();
os.close();
// Following as before
int responseCode = connection.getResponseCode();

Please use plain text.