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
Super Contributor
Posts: 308
Registered: ‎05-31-2011
My Device: Blackberry-SDK-9800
My Carrier: SimPakage 6.0.0.337_9800 asia.exe

validating problem with server

hello,

i am sending the login password to server like this:

private String getResult(String userID, String pwd)
{


String url="http://192.168.0.51/acusismg/User/Login";
String response = "";

ConnectionFactory connFact = new ConnectionFactory();

ConnectionDescriptor connDesc;


connDesc = connFact.getConnection(url);
InputStream input = null;
if (connDesc != null)
{
try{

HttpConnection httpConn;
httpConn = (HttpConnection)connDesc.getConnection();
httpConn.setRequestMethod(HttpConnection.POST);

httpConn.setRequestProperty("Content-Type","text/xml");

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {

e.printStackTrace();
}

Document mydocument = builder.newDocument();
Element _APPID = mydocument.createElement("userid");
Text TAPPID = mydocument.createTextNode(userID);
_APPID.appendChild(TAPPID);
_APPID.setNodeValue(userID);
mydocument.appendChild(_APPID);

Element _UID = mydocument.createElement("password");
Text TUID = mydocument.createTextNode(pwd);
_UID.appendChild(TUID);
_UID.setNodeValue(pwd);
mydocument.appendChild(_UID);



Element _PKEY = mydocument.createElement("timezoneid");
Text TPKEY = mydocument.createTextNode("India Standard Time");
_PKEY.appendChild(TPKEY);
_PKEY.setNodeValue("India Standard Time");
mydocument.appendChild(_PKEY);
mydocument.normalizeDocument();
ByteArrayOutputStream os = new ByteArrayOutputStream();
XMLWriter writer = new XMLWriter(os);
writer.setPrettyPrint();


DOMInternalRepresentation.parse(mydocument, writer);

String sss = os.toString();
String edit=sss.substring(21);
Dialog.alert(edit);
InputStream is = new ByteArrayInputStream(edit.getBytes("UTF-8"));
int length = (int) edit.length();
byte[] b = new byte[length];
is.read(b);
is.close();

httpConn.openOutputStream().write(b);



String timeZ=TimeZoneUtilities.getDisplayName(TimeZone.getDefault(), TimeZoneUtilities.SHORT);


httpConn = (HttpConnection)connDesc.getConnection();

final int iResponseCode = httpConn.getResponseCode();
Dialog.alert(Integer.toString(iResponseCode));
if (iResponseCode == HttpConnection.HTTP_OK)
{Dialog.alert("jdjddduf");

Dialog.alert("Response code: " + Integer.toString(iResponseCode));
input = httpConn.openInputStream();
byte[] data = new byte[1024];
int len;
StringBuffer raw = new StringBuffer();
while (-1 != (len = input.read(data)))
{
raw.append(new String(data, 0, len));
}

input.close();
httpConn.close();
response=raw.toString();
Dialog.alert(response);
}
}
catch(ConnectionNotFoundException e)
{
System.err.println("Caught IOException: " + e.getMessage());
}
catch(Exception e)
{
System.err.println("Caught IOException: " + e.getMessage());

}
}else{Dialog.alert("fff");}
return response;

}

 this code is not working plz check my code once..

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: validating problem with server

you cannot use Dialog.alert on a separate thread without invokelater (or similar).
if dialog.alert does not throw an exception it means you run the code on the event thread, which is bad. use a separate thread.
----------------------------------------------------------
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
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: validating problem with server

Once you have fixed that, how about giving us a little more detail on what is not working.  What line does it stop at, what have you tried, what does your server say etc etc..

Super Contributor
Posts: 308
Registered: ‎05-31-2011
My Device: Blackberry-SDK-9800
My Carrier: SimPakage 6.0.0.337_9800 asia.exe

Re: validating problem with server

ya, if i add content type as:

    httpConn.setRequestProperty("Content-Type","text/xml");

i am getting error code 412....

and if add content type as:

 httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

i am getting server error(500)....

As i am sending xml formate.... y i am getting error code 412.

 

Super Contributor
Posts: 308
Registered: ‎05-31-2011
My Device: Blackberry-SDK-9800
My Carrier: SimPakage 6.0.0.337_9800 asia.exe

Re: validating problem with server

hello,i have found the error, i didnt add the root to xml  i am sending to server... this problem is solved.... but now the data(userid and password i am sending) to server is associated with special charecter in front and end of userId when it receive the server,and my authuntication is failing again.... 

any clue about it

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

Re: validating problem with server

"special charecter in front "

 

Single character?  Or more than one?

 

Can you tell us exactly what the byte values are that you are receiving?

Super Contributor
Posts: 308
Registered: ‎05-31-2011
My Device: Blackberry-SDK-9800
My Carrier: SimPakage 6.0.0.337_9800 asia.exe

Re: validating problem with server

single char (rectangle block type) at front and end both side.... i dont know to name the char

Highlighted
Super Contributor
Posts: 308
Registered: ‎05-31-2011
My Device: Blackberry-SDK-9800
My Carrier: SimPakage 6.0.0.337_9800 asia.exe

Re: validating problem with server

this is the way i writ to outputstream:

InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
int length = (int) xmlString.length();
httpConn.setRequestMethod(HttpConnection.POST);
byte[] b = new byte[length];
is.read(b, 0, length);
is.close();

httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
httpConn.openOutputStream().write(b);

 this is method i am using to write to outputstream

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

Re: validating problem with server

[ Edited ]

The square box is how the bytes are being interpreted - in other words this is what is displayed.  Instead, can you look at the bytes that are involved.  We need the bytes.  For example if the byte contains x'31' then this is likely to be interpreted as the character 1. 

 

I find your code to create the bytes you send tortuous to say the least, however it all ends up here:

 

httpConn.openOutputStream().write(b);

 

So what are the contents of the first few bytes?  Use the debugger to find this out or search for a byte array to hex string conversion routine and dump them out in hex.