radityakurnianto

Loop

Nov 4th, 2014
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1. if(cursor.getCount() > 0) {
  2.                     cursor.moveToFirst();
  3.                     while (!cursor.isAfterLast()) {
  4.                         reqEntity.addPart(configuration.S_RECORD_USER_ID, cursor.getString(cursor.getColumnIndex(SQLiteHelper.RECORD_USER_ID)));
  5.                         reqEntity.addPart(configuration.S_RECORD_FORM_ID, cursor.getString(cursor.getColumnIndex(SQLiteHelper.RECORD_FORM_ID)));
  6.                         reqEntity.addPart(configuration.S_RECORD_JSON_DATA, cursor.getString(cursor.getColumnIndex(SQLiteHelper.RECORD_JSON_DATA)));
  7.                         reqEntity.addPart(configuration.S_LATITUDE, cursor.getString(cursor.getColumnIndex(SQLiteHelper.RECORD_LATITUDE)));
  8.                         reqEntity.addPart(configuration.S_LONGITUDE, cursor.getString(cursor.getColumnIndex(SQLiteHelper.RECORD_LONGITUDE)));
  9.                         reqEntity.addPart(configuration.S_CREATED_AT, cursor.getString(cursor.getColumnIndex(SQLiteHelper.RECORD_CREATED_AT)));
  10.                         reqEntity.addPart(configuration.S_LAST_UPDATE, cursor.getString(cursor.getColumnIndex(SQLiteHelper.RECORD_LAST_UPDATE)));
  11.  
  12.                         // proccess image here
  13.                         jsonObject = new JSONObject(cursor.getString(cursor.getColumnIndex(SQLiteHelper.RECORD_JSON_IMAGE)));
  14.                         Log.d("COBAIN", jsonObject.toString());
  15.                         if(jsonObject != null){
  16.                             jsonArray = jsonObject.getJSONArray("form_values_image");
  17.                             // loop through array length
  18.                             for(int i = 0; i < jsonArray.length(); i++){
  19.                                 jsonObject = jsonArray.getJSONObject(i);
  20.                                 // count the json object length here
  21.                                 if(jsonObject.length() > 0){
  22.                                     // loop through json object to convert img path to byte
  23.                                     for(int j = 0; j < jsonObject.length(); j++){
  24.                                         Log.d("PATH", jsonObject.getString("image_path_" + j));
  25.                                         photoToSend = processImage.decodeSampledBitmapFromFile(jsonObject.getString("image_path_" + i), 480, 480);
  26.                                         stream = new ByteArrayOutputStream();
  27.                                         photoToSend.compress(Bitmap.CompressFormat.PNG, 100, stream);
  28.                                         inputStream = new ByteArrayInputStream(stream.toByteArray());
  29.  
  30.                                         // image name format : created_date_formid.png
  31.                                         reqEntity.addPart("myFile", cursor.getString(cursor.getColumnIndex(SQLiteHelper.RECORD_CREATED_AT)).replace(":", "") + "_" + extras.getStringExtra("FORM_ID")  + ".png", inputStream);
  32.                                     }
  33.                                 }
  34.                             }
  35.                         }
  36.  
  37.                         httppost.setEntity(reqEntity);
  38.                         response = null;
  39.                         try {
  40.                             // begin post image to server
  41.                             response = httpclient.execute(httppost);
  42.                         } catch (ClientProtocolException e) {
  43.                             e.printStackTrace();
  44.                         } catch (IOException e) {
  45.                             e.printStackTrace();
  46.                         }
  47.                         try {
  48.                             if (response != null) {
  49.                                 if (response.getStatusLine().toString().equals("HTTP/1.1 200 OK")) {
  50.                                     try {
  51.                                         HttpEntity httpEntity = response.getEntity();
  52.                                         inputStream = httpEntity.getContent();
  53.                                         BufferedReader reader = new BufferedReader(new InputStreamReader(
  54.                                                 inputStream, "ISO-8859-1"), 8);
  55.                                         StringBuilder sb = new StringBuilder();
  56.                                         String line = null;
  57.                                         while ((line = reader.readLine()) != null) {
  58.                                             sb.append(line + "\n");
  59.                                         }
  60.                                         inputStream.close();
  61.                                         Log.d("JSONParser", sb.toString());
  62.                                     } catch (Exception e) {
  63.                                         Log.e("Buffer Error", "Error converting result " + e.toString());
  64.                                     }
  65.                                 }
  66.                                 Log.i("SERVER RESPONSE STATUS", response.getStatusLine().toString());
  67.                             }
  68.                         } finally {
  69.  
  70.                         }
  71.                         cursor.moveToNext();
  72.                     }
  73.                     cursor.close();
  74.                 }
Advertisement
Add Comment
Please, Sign In to add comment