String postRequest(String url) { HttpConnection hc = null; DataInputStream in = null; OutputStream os = null; try { hc = (HttpConnection) Connector.open(url); hc.setRequestMethod(HttpConnection.POST); hc.setRequestProperty("username", "bng"); hc.setRequestProperty("password", "123456"); os = hc.openDataOutputStream(); JSONObject jSONObject = new JSONObject(); jSONObject.put("start_count", 1); jSONObject.put("end_count", 10); jSONObject.put("type", 1); // os.write("{\"start_count\":\"1\",\"end_count\":\"12\"}".getBytes()); os.write(jSONObject.toString().getBytes()); // Always get the Response code first . int responseCode = hc.getResponseCode(); if (responseCode == HttpConnection.HTTP_OK) { // You have successfully connected. int length = (int) hc.getLength(); System.out.println("length : " + length); byte[] data = null; if (length != -1) { data = new byte[length]; in = new DataInputStream(hc.openInputStream()); in.readFully(data); } else { // If content length is not given, read in chunks. int chunkSize = 512; int index = 0; int readLength = 0; in = new DataInputStream(hc.openInputStream()); data = new byte[chunkSize]; do { if (data.length < index + chunkSize) { byte[] newData = new byte[index + chunkSize]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; } readLength = in.read(data, index, chunkSize); index += readLength; } while (readLength == chunkSize); length = index; } return new String(data); // Image image = Image.createImage(data, 0, length); // ImageItem imageItem = new ImageItem(null, image, 0, null); // mForm.append(imageItem); // mForm.setTitle("Done."); } else { // Problem with your connection Dialog.show(null, "error downloading images", "OK", null); return null; } } catch (Exception e) { e.printStackTrace(); Dialog.show(null, "error downloading images", "OK", null); return null; } finally { if (in != null) { try { in.close(); } catch (IOException ex) { ex.printStackTrace(); } } if (os != null) { try { os.close(); } catch (IOException ex) { ex.printStackTrace(); } } if (hc != null) { try { hc.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }