Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String formatedNumber(int number) {
- if (number < 10) {
- return "0" + number;
- } else {
- return String.valueOf(number);
- }
- }
- public void createBet() {
- Bet bet = new Bet();
- Intent intent = getIntent();
- bet.setUser(intent.getStringExtra(LoginActivity.USERNAME));
- bet.setTitle(binding.title.getText().toString());
- bet.setDescription(binding.description.getText().toString());
- Context context = getApplicationContext();
- CharSequence text;
- int duration = Toast.LENGTH_SHORT;
- if (bet.getTitle().isEmpty()) {
- text = "Title is empty";
- Toast toast = Toast.makeText(context, text, duration);
- toast.show();
- return;
- }
- if (bet.getDescription().isEmpty()) {
- text = "Description is empty";
- Toast toast = Toast.makeText(context, text, duration);
- toast.show();
- return;
- }
- bet.setData(formatedNumber(binding.data.getYear()) + "-" + formatedNumber(binding.data.getMonth()) +
- "-" + formatedNumber(binding.data.getDayOfMonth()));
- bet.setTime(formatedNumber(binding.time.getHour()) + ":" + formatedNumber(binding.time.getMinute()) +
- ":" + formatedNumber(binding.time.getMinute()));
- String choicesString = binding.choices.getText().toString();
- String[] choices = choicesString.split("\n");
- if (choices.length < 2) {
- text = "Not choice";
- Toast toast = Toast.makeText(context, text, duration);
- toast.show();
- return;
- }
- bet.setChoices(choices);
- Log.d("Title", bet.getTitle());
- Log.d("Description", bet.getDescription());
- Log.d("Data", bet.getData());
- Log.d("Time", bet.getTime());
- JSONObject jsonBet = new JSONObject();
- JSONArray jsonChoices = new JSONArray();
- try {
- jsonBet.put("title", bet.getTitle());
- jsonBet.put("description", bet.getDescription());
- jsonBet.put("deadline", bet.getData() + " " + bet.getTime());
- jsonBet.put("logo", "abc");
- jsonBet.put("user", bet.getUser());
- for (int i = 0; i < bet.getChoices().length; i++) {
- Log.d("asdas", bet.getChoices()[i]);
- JSONObject jsonChoice = new JSONObject();
- jsonChoice.put("description", bet.getChoices()[i]);
- jsonChoices.put(jsonChoice);
- }
- jsonBet.put("choises", jsonChoices);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- Log.d("JSON", jsonBet.toString());
- try {
- RequestQueue requestQueue = Volley.newRequestQueue(this);
- String URL = "http://91.225.131.195/lots.json";
- JSONObject jsonBody = jsonBet;
- final String requestBody = jsonBody.toString();
- StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.i("VOLLEY", response);
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.e("VOLLEY", error.toString());
- }
- }) {
- @Override
- public String getBodyContentType() {
- return "application/json; charset=utf-8";
- }
- @Override
- public byte[] getBody() throws AuthFailureError {
- try {
- return requestBody == null ? null : requestBody.getBytes("utf-8");
- } catch (UnsupportedEncodingException uee) {
- VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
- return null;
- }
- }
- @Override
- protected Response<String> parseNetworkResponse(NetworkResponse response) {
- String responseString = "";
- if (response != null) {
- responseString = String.valueOf(response.statusCode);
- // can get more details such as response.headers
- }
- return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
- }
- };
- requestQueue.add(stringRequest);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement