Nav apraksta

SendConsentForm.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*************************************************************
  2. * By: Coralys Cubero Rivera
  3. * Date: 2019
  4. *************************************************************/
  5. package uprrp.tania;
  6. import android.os.AsyncTask;
  7. import android.util.Log;
  8. import java.io.BufferedReader;
  9. import java.io.BufferedWriter;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.io.OutputStreamWriter;
  13. import java.io.UnsupportedEncodingException;
  14. import java.io.Writer;
  15. import java.net.URL;
  16. import java.net.URLConnection;
  17. import java.net.URLEncoder;
  18. import java.nio.charset.StandardCharsets;
  19. public class SendConsentForm extends AsyncTask <String, String, String> {
  20. @Override
  21. protected String doInBackground(String... strings) {
  22. String jsonConsentSignature = "";
  23. try {
  24. jsonConsentSignature = URLEncoder.encode("data", "UTF-8") + "=" + URLEncoder.encode(strings[0], "UTF-8");
  25. }
  26. catch (UnsupportedEncodingException e) {
  27. e.printStackTrace();
  28. }
  29. String serverReply = "";
  30. BufferedReader serverReader;
  31. //Send the signature to MARLE's server
  32. try
  33. {
  34. //Defined URL where to send data
  35. URL url = new URL("https://tania.uprrp.edu/getSignatureIOS.php");
  36. //Send POST data request
  37. URLConnection conn = url.openConnection();
  38. conn.setDoOutput(true);
  39. Writer writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
  40. writer.write(jsonConsentSignature);
  41. writer.close();
  42. InputStream inputStream = conn.getInputStream();
  43. if (inputStream == null) {
  44. return null;
  45. }
  46. //Get the server response
  47. serverReader = new BufferedReader(new InputStreamReader(inputStream));
  48. StringBuilder serverResponse = new StringBuilder();
  49. String serverResponseLine;
  50. // Read Server Response
  51. while((serverResponseLine = serverReader.readLine()) != null)
  52. {
  53. //Append server response in string
  54. serverResponse.append(serverResponseLine);
  55. }
  56. serverReply = serverResponse.toString();
  57. }
  58. catch(Exception ex) {
  59. Log.e("ERROR CONSENT SIGNATURE", ex.getMessage());
  60. }
  61. return serverReply;
  62. }
  63. @Override
  64. protected void onPostExecute(String s) {
  65. Log.d("CONSENT SERVER REPLY", s);
  66. }
  67. }