Most of the business application are web server based, meaning mostly they are targeted for optimizing the web application into mobile. Such kind of application mostly controlled from server. Mobile client always gets the data from server by making http/https connection and then reads the response data from bytes to string as required. Here is sample code snapshot will explain you to create connection and handle the response data. This piece of code is for blackberry deice, however we can use the same for j2me also. Here in my code sample make request string method creates a connection string by appending the required input parameters.
private String processRequest() { String url = makeRequestString(); int bufferCopyCount=0; HttpConnection c = null; InputStream is = null; OutputStream os = null; int rc; byte[] data = null; String exceptionMsg = null; try { c = (HttpConnection)Connector.open(url); c.setRequestProperty("User-Agent", DeviceInfo.getDeviceName() +" "+ DeviceInfo.getManufacturerName()); is = c.openInputStream(); os = c.openOutputStream(); os.flush(); rc = c.getResponseCode(); if (rc != HttpConnection.HTTP_OK) { throw new Exception(httpExceptionResponseCodeMsg(rc)); } int len = (int)c.getLength(); if (len > 0) { int actual = 0; int bytesread = 0 ; data = new byte[len]; while ((bytesread != len) && (actual != -1)) { actual = is.read(data, bytesread, len - bytesread); bytesread += actual; } for(int i=4;i>1;i--){ byte lowerByte = data[(len -i)]; byte higherByte = data[(len -i)+1]; if(lowerByte==13 && higherByte==10){ byte[] temp = new byte[(len-i)]; for (int j=0;j<(len-i);j++){ temp[j] = data[j]; } data = new byte[len-i]; data = temp; } } }else { int actual = 0; int bytesread =0; int loops =0; int dataBufferLastIndex = 0; byte[] buffer ; byte[] dataBuffer = new byte[1024 * 500]; while (actual != -1){ buffer = new byte[1024]; loops++; actual = is.read(buffer,0,1024); if(actual != -1){ bytesread += actual; int j=0; for(int i=dataBufferLastIndex;i<(actual+dataBufferLastIndex);i++){ dataBuffer[i] = buffer[j]; j++; } dataBufferLastIndex += actual; } } data = new byte[(bytesread +1)]; for(bufferCopyCount =0; bufferCopyCount<(data.length-1); bufferCopyCount++){ data[bufferCopyCount] = dataBuffer[bufferCopyCount]; } } } catch (Exception e) { ProgressScreen.removeProgress(); exceptionMsg = e.getMessage(); if(null == exceptionMsg || exceptionMsg.length() <= 0){ exceptionMsg = new String("Could not establish connection with TransAmerica Server. Please check the WIFI connection."); } } finally { if (is != null) try { is.close(); } catch (IOException e) { ProgressScreen.removeProgress(); e.printStackTrace(); } if (c != null) try { c.close(); } catch (IOException e) { ProgressScreen.removeProgress(); e.printStackTrace(); } } String response = exceptionMsg; if(data != null && data.length > 0){ try { response = new String(data, "UTF-8"); } catch (UnsupportedEncodingException e) { ProgressScreen.removeProgress(); response = "Unsupported encoding"; e.printStackTrace(); } } return response; }
