2013-02-27 100 views
-1

我想在我的黑莓應用程序中啓動一些線程,這取決於我從php獲取的響應。爲此,我想發送變量,就像我們在將URL參數從java發送到php時使用的那樣。這可以做到嗎?從php通信到java /使用黑莓手機獲取參數

+3

創建一個HTTP連接,然後郵寄/從/到URL獲得DATAS。 – Signare 2013-02-27 12:19:41

+0

謝謝Signare。你能否給我提供一些例子。我是新來的PHP。謝謝。 – catherine 2013-02-27 13:01:28

回答

0

試試這個代碼 -

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()); 

編輯 -

,如果你得到響應喜歡JSON考慮,然後將下面的代碼將幫助你分析它。 {"status":"100" "id":"123"}

JSONObject json = new JSONObject(res); 
String status=json.getString("status"); 
String id=json.getString("id"); 
+0

謝謝Signare。我知道這個代碼。但我想要一個可以幫助我通過名稱從php獲取變量的代碼。就像我們從Servelets中的JSP頁面獲得響應一樣。 response.getParameter( 「ID」);黑莓手機可以做到這一點嗎? – catherine 2013-03-01 05:34:12

+0

@凱瑟琳檢查我的答案的編輯部分。 – Signare 2013-03-01 05:41:09

+0

如果響應是xml,則可以解析它。 – Signare 2013-03-01 05:41:36