private void parseWallpaperThumbnailJSON(ContainerList containerList) { Image blank = Image.createImage(50, 50); try { JSONObject jSONObject = new JSONObject(wallpaperUrlResponse); System.out.println(jSONObject); Enumeration e = jSONObject.keys(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); // System.out.println(key + " : " + jSONObject.getString(key)); if (key.equalsIgnoreCase("wallpapers")) { JSONArray jSONArray = jSONObject.getJSONArray(key); for (int i = 0; i < jSONArray.length(); i++) { JSONObject wallpaper = jSONArray.getJSONObject(i); if (wallpaper.has("image_thumb_url") && wallpaper.has("image_category") && wallpaper.has("image_url") && wallpaper.has("wallpaper_price")) { // download the thumbnail and display it. Hashtable hashtable = new Hashtable(); hashtable.put("wallpaperThumbnailURL", wallpaper.getString("image_thumb_url")); hashtable.put("wallpaperURL", wallpaper.get("image_url")); hashtable.put("wallpaperPrice", "Price: " + wallpaper.get("wallpaper_price")); hashtable.put("wallpaperIcon", blank); // final ImageDownloadService ids = new // ImageDownloadService(wallpaper.getString("image_thumb_url"), cmp, // resultVector.size() - 1, "wallpaperIcon") { // protected void handleException(Exception err) { // if (err instanceof NullPointerException) { // System.out.println("NULL POINTER EXCEPTION // CAUGHT"); // return; // } // super.handleException(err); //To change body of // generated methods, choose Tools | Templates. // } // }; // NetworkManager.getInstance().addToQueue(ids); } } } } } catch (Exception ex) { System.out.println("error downloading images."); ex.printStackTrace(); Dialog.show(null, "error downloading images", "OK", null); } }
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(); } } } }