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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Contributor
Oloruntoba
Posts: 22
Registered: ‎05-04-2012
My Carrier: MTN Nigeria

post data to php via http

Hi all, I'm trying to post login information to a php page which in turn logs in and send a response back for the device to read. The problem is that even with the correct combination of username and password, i still get failure as response from the php instead of success.
Here are my codes:
Deviceside:
String userString = userField.getText();
String passField.getText();
URLEncodedPostData encodedData = new URLEncodedPostData(null, false);
encodedData.append("username", userString);
encodedData.append("password", passString);

HttpConnection connection = (HttpConnection)Conector.open(url);
Connection.setRequestMethod("POST");
Connection.setRequestProperty("Content-Type", "application/xwww-form-urlencoded");
Connection.setRequestProperty("Content-Length", (new Integer(postData.length)).toString());
OutputStream requestOutput = connection.openOutputStream();
RequestOutput.write(postData);
RequestOutput.close();

Int responseCode = connection.getResponseCode();
If (responseCode == HttpConnection.HTTP_OK) {
// get the response from server
}

Server side:
$user = $_POST['username'];
$password = $_POST['password'];
$select_user = mysql_query("SELECT * from users where username = '$user' and password = '$password'");

If (mysql_num_rows($select_user) != 0) {
echo ("Success");
}
else {
echo ("Failure");
}
}

I think the problem is that m not posting the values properly but i can't seem to identify where the flaw is. Please help
Please use plain text.
Developer
simon_hain
Posts: 13,802
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: post data to php via http

first: do not execute networking on the event thread. see http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

i would suggest that you check your output using a local proxy such as tcpmon.
----------------------------------------------------------
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
Please use plain text.
Contributor
Oloruntoba
Posts: 22
Registered: ‎05-04-2012
My Carrier: MTN Nigeria

Re: post data to php via http

The networking is not on the event thread... I just extracted the code from my posthttprequest thread. I will try to check the output. Thanks
Please use plain text.
Developer
pankajace12
Posts: 188
Registered: ‎04-30-2011
My Carrier: Airtel

Re: post data to php via http

Hi 

 

Pleas try this code. put your username password value as a parameter.

 

	public static boolean useraccount(String username,String password)
    {
	    boolean ret = false;
        InputStream inputStream = null;
        HttpConnection httpConnection = null;
    
        try
        {
            
            StringBuffer returnStringBuffer = new StringBuffer();
            String returnString = new String();
            

            String desiredEncoding = "ISO-8859-1";


            URLEncodedPostData params = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, true);
        
          
            params.append("username", username);
            params.append("password", password);
               
            String url = "<YOUR URL>" + params.toString();                     
                                       
            //Connecting to Server
            httpConnection = (HttpConnection)Connector.open(url);
            
            inputStream = httpConnection.openDataInputStream();
            
            if(httpConnection.getResponseCode() == HttpConnection.HTTP_OK)
            {
                int ch;
            
                //Process Response
                
                // check header field for a specific encoding
                String contenttype = httpConnection.getHeaderField("Content-Type");
                if (contenttype != null)
                {
                    contenttype = contenttype.toUpperCase();
                    if (contenttype.indexOf("UTF-8") != -1)
                    {
                        desiredEncoding = "UTF-8";
                    }
                }
            
                // get an inputstreamreader to handle utf-8 data
                InputStreamReader isr = new InputStreamReader(inputStream,desiredEncoding);
            
                while ((ch = isr.read()) != -1) 
                {
                    returnStringBuffer.append((char) ch);
                }
                    
                inputStream.close();
                httpConnection.close();
                inputStream = null;
                httpConnection = null;
                returnString = returnStringBuffer.toString();

                // examine return string
                if (returnString.indexOf("SUCCESS") != -1)
                {
                    ret = true;
                }
                return ret;
            }
            inputStream.close();
            httpConnection.close();
            inputStream = null;
            httpConnection = null;
            //Bad Transaction.
            return ret;
        }
        catch (Exception e)
        {
            System.out.println("Error occurred in ProcessTransaction(,)\n" + e.toString());
            return ret;
        }
        finally
        {
            try
            {
                if (inputStream != null)
                    inputStream.close();
                if (httpConnection != null)
                    httpConnection.close();
            }
            catch (Exception ee)
            {
            }
        }
   }
	

 

 

Thanks

Pawan

Please use plain text.
Contributor
Oloruntoba
Posts: 22
Registered: ‎05-04-2012
My Carrier: MTN Nigeria

Re: post data to php via http

@pankajes12; thanks for the code... Same output though. Its returning false.

If it'll help, here's the link to the php file http://www.onadabayi.x10.mx/SociaLink/test.php
A form that posts to the test.php is located at www.onadabayi.x10.mx/SociaLink/login.html

A valid username=daniel and password= daniel
Please use plain text.
Contributor
Oloruntoba
Posts: 22
Registered: ‎05-04-2012
My Carrier: MTN Nigeria

Re: post data to php via http

 
Please use plain text.
Developer
pankajace12
Posts: 188
Registered: ‎04-30-2011
My Carrier: Airtel

Re: post data to php via http

HI

Can you check, what url is exactly passed to server?

Thanks
Please use plain text.
Contributor
Oloruntoba
Posts: 22
Registered: ‎05-04-2012
My Carrier: MTN Nigeria

Re: post data to php via http

Please use plain text.
Developer
pankajace12
Posts: 188
Registered: ‎04-30-2011
My Carrier: Airtel

Re: post data to php via http

hi, if you will put it on browser. it is also not working.

So confirm your url first.

Thanks
Please use plain text.
Developer
simon_hain
Posts: 13,802
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: post data to php via http


Oloruntoba wrote:
Yes... "http://www.onadabayi.x10.mx/test.phpusername=daniel&password=daniel"

This is not the same as sending a POST request with username/password, just saying.

If your webservice requires this format a simple GET would be sufficient.

 

----------------------------------------------------------
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
Please use plain text.