I want to start some threads in my blackberry application based on the response I get from the php I ping. For this, I want to send variables, like we use when we send URL parameters from java to php. Can this be done?
Asked
Active
Viewed 247 times
-1
-
3Create a http connection , then post/get datas from/to the url. – Rince Thomas Feb 27 '13 at 12:19
-
Thanks Signare. Can you please provide me some examples. I'm new to php. Thanks. – catherine Feb 27 '13 at 13:01
2 Answers
0
You need to make an http call to your php service, and after you get an response treat it accordingly, you should go with http client from apache, this will help you: How to call and pass the parameters to the Servlet using the Java class in my swing application?
0
Try this code-
String httpURL="http://www.your_url.com/login.php?username=123&password=123";
HttpConnection httpConn;
httpConn = (HttpConnection) Connector.open(httpURL);
httpConn.setRequestMethod(HttpConnection.POST);
DataOutputStream _outStream = new ataOutputStream(httpConn.openDataOutputStream());
byte[] request_body = httpURL.getBytes();
for (int i = 0; i < request_body.length; i++) {
_outStream.writeByte(request_body[i]);
}
DataInputStream _inputStream = new DataInputStream(
httpConn.openInputStream());
StringBuffer _responseMessage = new StringBuffer();
int ch;
while ((ch = _inputStream.read()) != -1) {
_responseMessage.append((char) ch);
}
String res = (_responseMessage.toString());
Edit-
Consider if you get response like json, then the following code will help you in parsing it.
{"status":"100" "id":"123"}
JSONObject json = new JSONObject(res );
String status=json.getString("status");
String id=json.getString("id");

Rince Thomas
- 4,158
- 5
- 25
- 44
-
Thanks Signare. I know this code. But I wanted a code which would help me get variables by name from php. Like we get response from JSP page in Servelets eg. response.getParameter("id"); Can this be done for Blackberry ? – catherine Mar 01 '13 at 05:34
-
-
Thanks once again Signare. So, you mean that I have to "echo" a string like you've mentioned: {"status":"100" "id":"123"} and then parse it as with JSONObject ? – catherine Mar 01 '13 at 06:12
-