unable to perform request - response
I wrote the following code for client - server communication in Blackberry but its not working.
I am sending data via POST.
I am not receiving any response from the server while testing using Blackberry simulator.
With Android & iPhone , I am able to get the response from the same server url & with the same request parameters.
private void communicate() {
HttpConnection hc = null;
DataInputStream dis = null;
DataOutputStream dos = null;
StringBuffer messagebuffer = new StringBuffer();
try{
String input="firstname="+ fName.getText().trim()+
"&lastname=" + lName.getText().trim()+"&platform=blackberry";
String url = "http://127.0.0.1:80/index/login";
hc = (HttpConnection)
Connector.open(url, Connector.READ_WRITE); // Set the request method
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("User-Agent", "BlackBerry");
hc.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
hc.setRequestProperty("Cont开发者_JAVA百科ent-Length",
Integer.toString(input.length()));
dos = hc.openDataOutputStream();
dos.write(input.getBytes());
dos.flush(); dos.close();
// Retrieve the response back from the servlet
dis = new DataInputStream(hc.openInputStream());
int ch;
// Check the Content-Length first
long len = hc.getLength();
if(len!=-1) {
for(int i = 0;i<len;i++)
if((ch = dis.read())!= -1)
messagebuffer.append((char)ch);
} else { // if the content-length is not available
while ((ch = dis.read()) != -1)
messagebuffer.append((char) ch);
}
dis.close();
}catch(Exception e){
e.printStackTrace();
}
}
Kindly suggest if I need to make any changes in the code.
Thanks in advance.
CB
The MDS server will not allow a connection to 127.0.0.1 or localhost.
精彩评论