private void saveData() { reg.addValue("SELECT" + depto.getId(), depto.getSelectedItem().toString()); reg.addValue("SELECT" + province.getId(), province.getSelectedItem().toString()); reg.addValue("SELECT" + district.getId(), district.getSelectedItem().toString()); reg.addValue("SELECT" + station.getId(), station.getSelectedItem().toString()); for (Modulo m : modulos) { for (cl.tdc.felipe.tdc.objects.Relevar.Item i : m.getItems()) { View v = i.getVista(); View c = i.getDescription(); if (i.getCheckBoxes() != null) { for (int x = 0; x < i.getCheckBoxes().size(); x++) { CheckBox check = i.getCheckBoxes().get(x); reg.addValue("CHECK" + check.getId(), check.isChecked()); if (x == 0) reg.addValue("COMMENTCHECK" + check.getId(), ((EditText) c).getText().toString()); } } if (v instanceof Spinner) { reg.addValue("SELECT" + v.getId(), ((Spinner) v).getSelectedItem().toString()); reg.addValue("COMMENTSELECT" + v.getId(), ((EditText) c).getText().toString()); } if (v instanceof EditText) { reg.addValue("TEXT" + v.getId(), ((EditText) v).getText().toString()); if (c != null) reg.addValue("COMMENTTEXT" + v.getId(), ((EditText) c).getText().toString()); } } } for (FormImage img : imagenes) { if (img.getImage() != null) { String id1 = "FOTOBITMAP" + img.getIdSystem() + img.getIdSubSystem(); String id2 = "FOTONAME" + img.getIdSystem() + img.getIdSubSystem(); String id3 = "FOTOCOMMENT" + img.getIdSystem() + img.getIdSubSystem(); reg.addValue(id1, Funciones.encodeTobase64(img.getImage())); reg.addValue(id2, img.getName()); reg.addValue(id3, img.getComment()); } } }
@Override protected String doInBackground(String... params) { String response = ""; for (FormImage img : imagenes) { try { String fileName = img.getName(); publishProgress(fileName); Log.i("ENVIANDO", fileName); Log.i("ENVIANDO", "comentario: " + img.getComment()); Log.i("ENVIANDO", "system: " + img.getIdSystem()); Log.i("ENVIANDO", "subsystem: " + img.getIdSubSystem()); HttpURLConnection conn; DataOutputStream dos; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; File done = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), fileName); done.createNewFile(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); img.getImage().compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray(); FileOutputStream fos = new FileOutputStream(done); fos.write(bitmapdata); fos.flush(); fos.close(); if (!done.isFile()) Log.e("DownloadManager", "no existe"); else { FileInputStream fileInputStream = new FileInputStream(done); URL url = new URL(dummy.URL_R_SEND_CHECK_IMGS); conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("uploaded_file", fileName); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes( "Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, 1 * 1024 * 1024); byte[] buf = new byte[bufferSize]; bytesRead = fileInputStream.read(buf, 0, bufferSize); while (bytesRead > 0) { dos.write(buf, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, 1 * 1024 * 1024); bytesRead = fileInputStream.read(buf, 0, bufferSize); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); int serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn.getResponseMessage(); Log.i( "UploadManager", "HTTP response is: " + serverResponseMessage + ": " + serverResponseCode); fileInputStream.close(); dos.flush(); dos.close(); InputStream responseStream = new BufferedInputStream(conn.getInputStream()); BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream)); String line = ""; StringBuilder stringBuilder = new StringBuilder(); while ((line = responseStreamReader.readLine()) != null) { stringBuilder.append(line).append("\n"); } responseStreamReader.close(); response = stringBuilder.toString(); if (response.contains("<MESSAGE>OK</MESSAGE>")) { Log.d("ENVIANDO", "OK"); img.setSend(true); } else { Log.d("ENVIANDO", "NOK"); } } } catch (Exception e) { Log.d("TAG", "Error: " + e.getMessage()); response = "ERROR"; } } return response; }