Blackberry sends an HTTPPost request

I am developing a blackberry application and I need to send an http request to my server. I am using a simulator to test my application, and I found this code to send a request:

http://vasudevkamath.techfiz.com/general/posting-data-via-http-from-blackberry/

But I can not get it to work, because it does not work on this line:

int rc = _httpConnection.getResponseCode(); 

Any idea?

thanks

+3
source share
5 answers

Here is a sample code on how to send a POST request:

 HttpConnection c = (HttpConnection) Connector.open(url, Connector.READ_WRITE); c.setRequestMethod(HttpConnection.POST); OutputStream os = c.openOutputStream(); os.write(request.getBytes("UTF-8")); os.flush(); os.close(); InputStream is = c.openInputStream(); 

Just make sure you use this code in a separate thread.

+2
source
 public static ResponseBean sendRequestAndReceiveResponse(String method, String absoluteURL, String bodyData, boolean readResponseBody) throws IOException { ResponseBean responseBean = new ResponseBean(); HttpConnection httpConnection = null; try { String formattedURL = absoluteURL + "deviceside=true;interface=wifi"; // If you are using WiFi //String formattedURL = absoluteURL + "deviceside=false"; // If you are using BES //String formattedURL = absoluteURL + "deviceside=true"; // If you are using TCP if(DeviceInfo.isSimulator()) // if you are using simulator formattedURL = absoluteURL; httpConnection = (HttpConnection) Connector.open(formattedURL); httpConnection.setRequestMethod(method); if (bodyData != null && bodyData.length() > 0) { OutputStream os = httpConnection.openOutputStream(); os.write(bodyData.getBytes("UTF-8")); } int responseCode = httpConnection.getResponseCode(); responseBean.setResponseCode(responseCode); if (readResponseBody) { responseBean.setBodyData(readBodyData(httpConnection)); } } catch (IOException ex) { System.out.println("!!!!!!!!!!!!!!! IOException in NetworkUtil::sendRequestAndReceiveResponse(): " + ex); throw ex; } catch(Exception ex) { System.out.println("!!!!!!!!!!!!!!! Exception in NetworkUtil::sendRequestAndReceiveResponse(): " + ex); throw new IOException(ex.toString()); } finally { if (httpConnection != null) httpConnection.close(); } return responseBean; } public static StringBuffer readBodyData(HttpConnection httpConnection) throws UnsupportedEncodingException, IOException { if(httpConnection == null) return null; StringBuffer bodyData = new StringBuffer(256); InputStream inputStream = httpConnection.openDataInputStream(); byte[] data = new byte[256]; int len = 0; int size = 0; while ( -1 != (len = inputStream.read(data)) ) { bodyData.append(new String(data, 0, len,"UTF-8")); size += len; } if (inputStream != null) { inputStream.close(); } return bodyData; } 
+2
source

I know this question is quite old, and the OP has probably solved it so far, but I just ran into the same problem and was able to fix it!

You need to add ;deviceside=true to your URL.

So, for example, your URL will change from "http://example.com/directory/submitpost.php" to "http://example.com/directory/submitpost.php;deviceside=true" .

I found this here: http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-connection/ta-p/445879

My POST request was disconnected after 3 minutes when I didn’t have this (see my comment ), but it works fine with this URL app.

I would also recommend using ConnectionFactory . Here are some of my code:

 Network.httpPost("http://example.com/directory/submitpost.php;deviceside=true", paramNamesArray, paramValsArray) 
 public static void httpPost(String urlStr, String[] paramName, String[] paramVal) throws Exception { ConnectionFactory conFactory = new ConnectionFactory(); conFactory.setTimeLimit(1000); HttpConnection conn = (HttpConnection) conFactory.getConnection(urlStr).getConnection(); conn.setRequestMethod(HttpConnection.POST); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); StringBuffer sb = new StringBuffer(); for (int i = 0; i < paramName.length; i++) { sb.append(paramName[i]); sb.append("="); sb.append(paramVal[i]); sb.append("&"); } byte[] postData = sb.toString().getBytes("UTF-8"); conn.setRequestProperty("Content-Length",new Integer(postData.length).toString()); OutputStream out = conn.openOutputStream(); out.write(postData); //out.flush(); //Throws an Exception for some reason/Doesn't do anything anyways out.close(); //This writes to our connection and waits for a response if (conn.getResponseCode() != 200) { throw new Exception(conn.getResponseMessage()); } } 
+1
source

Not sure about your site, but I have successfully used the ConnectionFactory sample provided on the Blackberry website.

http://supportforums.blackberry.com/t5/Java-Development/Sample-Code-Using-the-ConnectionFactory-class-in-a-BrowserField/ta-p/532860

Just remember to hook into the EventThread event.

0
source

As you add parameters, Full answer here:

 StringBuffer postData = new StringBuffer(); httpConn = (HttpConnection) Connector.open("https://surveys2.kenexa.com/feedbacksurveyapi/login?"); httpConn.setRequestMethod(HttpConnection.POST); postData.append("username="+username); postData.append("&password="+pass); postData.append("&projectcode="+projectid); String encodedData = postData.toString(); httpConn.setRequestProperty("Content-Language", "en-US"); httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); httpConn.setRequestProperty("Content-Length",(new Integer(encodedData.length())).toString()); byte[] postDataByte = postData.toString().getBytes("UTF-8"); OutputStream out = httpConn.openOutputStream(); out.write(postDataByte); out.close(); httpConn.getResponseCode(); 
0
source

All Articles