I am developing an application where i will need to collect user-unput and send to a PHP script on the server.I searched and got some code which showed me how to use a class for posting the data ...but the data does not get posted to the server.Where am i missing it?..Below is some of my code(N.B:I AM TESTING FROM A SIMULATOR USING MDS)..
Code:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
/**
*
*/
class IntroPage extends MainScreen{
// private members - these represent the "controls"
private BasicEditField identifierfield = null;
private BasicEditField datafield = null;
private ButtonField submitbutton = null;
private LabelField statusfield = null;
IntroPage() {
super();
setTitle("BB IBM Demo App");
identifierfield = new BasicEditField("Identifier: ","",50, EditField.NO_NEWLINE );
// add this field to the screen
add(identifierfield);
// create a field for the data of our transaction
datafield = new BasicEditField("Data: ","",100, EditField.NO_NEWLINE );
// add this field to the screen
add(datafield);
// createlistener
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ButtonField submitbutton = (ButtonField) field;
String id=identifierfield.getText();
String data=datafield.getText();
if(id.trim().length()==0||data.trim().length()==0)
{
Dialog.alert("Please Fill In All Fields.");
return; }
if(bb_ibm_transaction.ProcessTransaction(id,data)==true)
{
Dialog.alert("Your Data Has Been Saved Successfully"); }
else
{
Dialog.alert("There Was An Error Saving Your Details,Please Try Again Later."); }
}
};
// create a button to submit our transaction
submitbutton = new ButtonField("Submit Transaction",ButtonField.CONSUME_CLICK);
submitbutton.setChangeListener(listener);
// add this button to the screen
add(submitbutton);
// add a status label
statusfield = new LabelField("Please enter transaction data.");
When i click on the submit button..i get the "There was an error saving your details" dialogue...(which means the connection class does not echo 'success'...thereby returns false) Below is the code for the Connection class:
Code:
import net.rim.blackberry.api.browser.URLEncodedPostData;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
/**
*
*/
class bb_ibm_transaction {
bb_ibm_transaction() { }
// this method interacts with the server
public static boolean ProcessTransaction(String id,String data)
{
// default to non-success return code
boolean ret = false;
// some variables necessary for HTTP communication
InputStream inputStream = null;
HttpConnection httpConnection = null;
// because many of the steps can throw an exception, wrap this method in try/catch block
try
{
StringBuffer returnStringBuffer = new StringBuffer();
String returnString = new String();
String desiredEncoding = "ISO-8859-1";
URLEncodedPostData params = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, true);
params.append("identifier", id);
params.append("data", data);
String url = "htxx://localhost/mob/test.php?" + params.toString();
System.out.println(url);
//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(" + id + "," + data + ")\n" + e.toString());
return ret;
}
finally
{
try
{
if (inputStream != null)
inputStream.close();
if (httpConnection != null)
httpConnection.close();
}
catch (Exception ee)
{
}
}
}
}
And this is the php script
Code:
<?php
include 'mob_connect.php';
$id=mysqli_real_escape_string($link,$_POST['id']);
if($id)
{
echo "SUCCESS";
}
?>
Please Where Am I Missing It?