Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. public String formatedNumber(int number) {
  2. if (number < 10) {
  3. return "0" + number;
  4. } else {
  5. return String.valueOf(number);
  6. }
  7. }
  8.  
  9. public void createBet() {
  10. Bet bet = new Bet();
  11. Intent intent = getIntent();
  12. bet.setUser(intent.getStringExtra(LoginActivity.USERNAME));
  13. bet.setTitle(binding.title.getText().toString());
  14. bet.setDescription(binding.description.getText().toString());
  15.  
  16. Context context = getApplicationContext();
  17. CharSequence text;
  18. int duration = Toast.LENGTH_SHORT;
  19.  
  20. if (bet.getTitle().isEmpty()) {
  21. text = "Title is empty";
  22. Toast toast = Toast.makeText(context, text, duration);
  23. toast.show();
  24. return;
  25. }
  26.  
  27. if (bet.getDescription().isEmpty()) {
  28. text = "Description is empty";
  29. Toast toast = Toast.makeText(context, text, duration);
  30. toast.show();
  31. return;
  32. }
  33.  
  34. bet.setData(formatedNumber(binding.data.getYear()) + "-" + formatedNumber(binding.data.getMonth()) +
  35. "-" + formatedNumber(binding.data.getDayOfMonth()));
  36. bet.setTime(formatedNumber(binding.time.getHour()) + ":" + formatedNumber(binding.time.getMinute()) +
  37. ":" + formatedNumber(binding.time.getMinute()));
  38.  
  39. String choicesString = binding.choices.getText().toString();
  40.  
  41. String[] choices = choicesString.split("\n");
  42.  
  43. if (choices.length < 2) {
  44. text = "Not choice";
  45. Toast toast = Toast.makeText(context, text, duration);
  46. toast.show();
  47. return;
  48. }
  49.  
  50. bet.setChoices(choices);
  51.  
  52. Log.d("Title", bet.getTitle());
  53. Log.d("Description", bet.getDescription());
  54. Log.d("Data", bet.getData());
  55. Log.d("Time", bet.getTime());
  56.  
  57. JSONObject jsonBet = new JSONObject();
  58. JSONArray jsonChoices = new JSONArray();
  59.  
  60. try {
  61. jsonBet.put("title", bet.getTitle());
  62. jsonBet.put("description", bet.getDescription());
  63. jsonBet.put("deadline", bet.getData() + " " + bet.getTime());
  64. jsonBet.put("logo", "abc");
  65. jsonBet.put("user", bet.getUser());
  66.  
  67. for (int i = 0; i < bet.getChoices().length; i++) {
  68. Log.d("asdas", bet.getChoices()[i]);
  69. JSONObject jsonChoice = new JSONObject();
  70. jsonChoice.put("description", bet.getChoices()[i]);
  71. jsonChoices.put(jsonChoice);
  72. }
  73. jsonBet.put("choises", jsonChoices);
  74.  
  75. } catch (JSONException e) {
  76. e.printStackTrace();
  77. }
  78. Log.d("JSON", jsonBet.toString());
  79.  
  80. try {
  81. RequestQueue requestQueue = Volley.newRequestQueue(this);
  82. String URL = "http://91.225.131.195/lots.json";
  83. JSONObject jsonBody = jsonBet;
  84. final String requestBody = jsonBody.toString();
  85.  
  86. StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
  87. @Override
  88. public void onResponse(String response) {
  89. Log.i("VOLLEY", response);
  90.  
  91. }
  92. }, new Response.ErrorListener() {
  93. @Override
  94. public void onErrorResponse(VolleyError error) {
  95. Log.e("VOLLEY", error.toString());
  96. }
  97. }) {
  98. @Override
  99. public String getBodyContentType() {
  100. return "application/json; charset=utf-8";
  101. }
  102.  
  103. @Override
  104. public byte[] getBody() throws AuthFailureError {
  105. try {
  106. return requestBody == null ? null : requestBody.getBytes("utf-8");
  107. } catch (UnsupportedEncodingException uee) {
  108. VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
  109. return null;
  110. }
  111. }
  112.  
  113. @Override
  114. protected Response<String> parseNetworkResponse(NetworkResponse response) {
  115. String responseString = "";
  116. if (response != null) {
  117. responseString = String.valueOf(response.statusCode);
  118. // can get more details such as response.headers
  119. }
  120. return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
  121. }
  122. };
  123.  
  124. requestQueue.add(stringRequest);
  125. } catch (Exception e) {
  126. e.printStackTrace();
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement