Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. package com.example.tryingapp;
  2.  
  3. import java.io.IOException;
  4.  
  5. import okhttp3.FormBody;
  6. import okhttp3.OkHttpClient;
  7. import okhttp3.Request;
  8. import okhttp3.Response;
  9.  
  10.  
  11. /**
  12. * Created by DELL on 20/02/2017.
  13. */
  14.  
  15. public class ConnectAndCommunicateWithServer {
  16.  
  17. static OkHttpClient client = new OkHttpClient();
  18. final String BasicUrl = "http://aviyam1811.pythonanywhere.com";
  19.  
  20. boolean updateFCMId(String FCMId , String mail) throws IOException {
  21. FormBody formBody = new FormBody.Builder()
  22. .add("FCMId", FCMId)
  23. .add("mail", mail)
  24. .build();
  25. okhttp3.Request request = new Request.Builder()
  26. .url(BasicUrl + "/updatefcmid")
  27. .post(formBody)
  28. .build();
  29. Response response = client.newCall(request).execute();
  30. //TODO: look about the problem here
  31. System.out.println(response.body().string());
  32. return response.body().string().contains("Update FCMId succeeded!");
  33. }
  34.  
  35. boolean register(String pass, String mail , String FCMId) throws IOException {
  36. FormBody formBody = new FormBody.Builder()
  37. .add("mail", mail)
  38. .add("password", pass)
  39. .add("FCMId" , FCMId)
  40. .build();
  41. okhttp3.Request request = new Request.Builder()
  42. .url(BasicUrl + "/register")
  43. .post(formBody)
  44. .build();
  45. Response response = client.newCall(request).execute();
  46. return response.body().string().equals("Signup completed!");
  47. }
  48.  
  49. boolean addNewContact(String mail , String additionalMail , String isFull) throws IOException {
  50. FormBody formBody = new FormBody.Builder()
  51. .add("additionalMail", additionalMail)
  52. .add("mail", mail)
  53. .add("isFull" , isFull)
  54. .build();
  55. okhttp3.Request request = new Request.Builder()
  56. .url(BasicUrl + "/addnewcontact")
  57. .post(formBody)
  58. .build();
  59. Response response = client.newCall(request).execute();
  60. return response.body().string().contains("contact adding succeeded!");
  61. }
  62.  
  63. boolean changeSO(String mail , String additionalMail , String isFull) throws IOException {
  64. FormBody formBody = new FormBody.Builder()
  65. .add("additionalMail", additionalMail)
  66. .add("mail", mail)
  67. .add("isFull" , isFull)
  68. .build();
  69. okhttp3.Request request = new Request.Builder()
  70. .url(BasicUrl + "/changeso")
  71. .post(formBody)
  72. .build();
  73. Response response = client.newCall(request).execute();
  74. System.out.println(response.body().string());//TODO remove just for debug
  75. return response.body().string().contains("succeeded");
  76. }
  77.  
  78. boolean deleteContact(String mail , String additionalMail) throws IOException {
  79. FormBody formBody = new FormBody.Builder()
  80. .add("additionalMail", additionalMail)
  81. .add("mail", mail)
  82. .build();
  83. okhttp3.Request request = new Request.Builder()
  84. .url(BasicUrl + "/deletecontact")
  85. .post(formBody)
  86. .build();
  87. Response response = client.newCall(request).execute();
  88. System.out.println(response.body().string());
  89. return response.body().string().contains("succeeded!");//TODO check why return false
  90. }
  91.  
  92. boolean emergencyCase(String mail) throws IOException {
  93. FormBody formBody = new FormBody.Builder()
  94. .add("mail", mail)
  95. .build();
  96. okhttp3.Request request = new Request.Builder()
  97. .url(BasicUrl + "/emergencycase")
  98. .post(formBody)
  99. .build();
  100. Response response = client.newCall(request).execute();
  101. System.out.println(response.body().string());
  102. return response.body().string().equals("true");//TODO check why not notify
  103. }
  104.  
  105. boolean fullCase(String mail) throws IOException {
  106. FormBody formBody = new FormBody.Builder()
  107. .add("mail", mail)
  108. .build();
  109. okhttp3.Request request = new Request.Builder()
  110. .url(BasicUrl + "/fullcase")
  111. .post(formBody)
  112. .build();
  113. Response response = client.newCall(request).execute();
  114. return response.body().string().equals("true");
  115. }
  116.  
  117. //just for debug
  118. public static void main(String[] args) throws IOException {
  119.  
  120. ConnectAndCommunicateWithServer example = new ConnectAndCommunicateWithServer();
  121. // issue the post request - register
  122. //System.out.println(example.register("fromMainIntryingApp2" , "Good"));
  123. //System.out.println(example.addNewContact("aviyam1811@walla.com" ,"yael1709@gmail.com" , "true"));
  124. //System.out.println(example.changeSO("ivgi1234@walla.com" ,"xhdhdhh@hdjdhdb.com" , "good"));
  125. //System.out.println(example.deleteContact("aviyam1811@walla.com" ,"yael1709@gmail.com"));
  126. System.out.println(example.emergencyCase("aviyam1811@walla.com"));
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement