Advertisement
Guest User

Untitled

a guest
Sep 12th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.95 KB | None | 0 0
  1. package cz.videotechnik.videotechnik.ui;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9.  
  10. import com.google.gson.JsonObject;
  11.  
  12. import java.util.HashMap;
  13. import java.util.Map;
  14.  
  15. import cz.videotechnik.videotechnik.User.*;
  16. import cz.videotechnik.videotechnik.R;
  17. import cz.videotechnik.videotechnik.service.Api;
  18. import retrofit2.Call;
  19. import retrofit2.Callback;
  20. import retrofit2.Response;
  21.  
  22.  
  23. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  24.     Api apiRoutes;
  25.     ApiClient ApiClient;
  26.  
  27.     private Button b_get;
  28.     private Button b_post;
  29.     private Button b_login;
  30.     private TextView tv_output;
  31.     private  EditText et_action;
  32.     private  EditText et_UserName;
  33.     private  EditText et_Password;
  34.     private static final String LOGIN_URL = "https://www.videotechnik.cz/";
  35.  
  36.     private static final String USER_ACTION_TEST = "testLogin";
  37.     private static final String USER_ALEXANDER_TEST = "Alexander";
  38.     private static final String USER_PASSWORD_TEST = "123456789";
  39.  
  40.  
  41.  
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_main);
  46.  
  47.         b_get = (Button) findViewById(R.id.b_Get);
  48.         b_post = (Button) findViewById(R.id.b_Post);
  49.         b_login = (Button) findViewById(R.id.b_login);
  50.         tv_output = (TextView) findViewById(R.id.tv_output);
  51.         et_UserName = (EditText) findViewById(R.id.et_UserName);
  52.         et_Password = (EditText) findViewById(R.id.et_Password);
  53.         et_action = (EditText) findViewById(R.id.et_action);
  54.  
  55.  
  56.         apiRoutes = ApiClient.getClient().create(Api.class);
  57.  
  58. //      ------- START -----   For POST
  59.  
  60.         final HashMap<String, String> paramNameOfUserAndLogin = new HashMap<>();
  61.         paramNameOfUserAndLogin.put("action",USER_ACTION_TEST);
  62.         paramNameOfUserAndLogin.put("user", USER_ALEXANDER_TEST);
  63.  
  64.  
  65.         final View.OnClickListener onClickListener_Login = new View.OnClickListener() {
  66.             @Override
  67.             public void onClick(View v) {
  68.                 apiRoutes.loginAccount(paramNameOfUserAndLogin).enqueue(new Callback<JsonObject>() {
  69.                     @Override
  70.                     public void onResponse(Call<JsonObject> call, Response<JsonObject> responseQ) {
  71.                         tv_output.setText("Response POST:\n" + responseQ.body().toString());
  72.                     }
  73.  
  74.                     @Override
  75.                     public void onFailure(Call<JsonObject> call, Throwable t) {
  76.  
  77.                     }
  78.                 });
  79.             }
  80.         };
  81.         b_login.setOnClickListener(onClickListener_Login);
  82.  
  83. //    -----  END ------   For POST
  84.  
  85.  
  86.  
  87.  
  88.  
  89. //      ------- START -----   For GET
  90.         final Map<String, String> params = new HashMap<>();
  91.         params.put("action","testRequest");
  92.         params.put("anyparam","anyvalue");
  93.  
  94.         final View.OnClickListener onClickListener_Get = new View.OnClickListener() {
  95.             @Override
  96.             public void onClick(View v) {
  97.                 apiRoutes.getParametrs(params).enqueue(new Callback<JsonObject>() {
  98.  
  99.                     @Override
  100.                     public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
  101.                         tv_output.setText("Response GET:\n" + response.body().toString());
  102.                     }
  103.  
  104.                     @Override
  105.                     public void onFailure(Call<JsonObject> call, Throwable t) {
  106.  
  107.                     }
  108.                 });
  109.             }
  110.         };
  111.         b_get.setOnClickListener(onClickListener_Get);
  112. //      ------- END -----   For GET
  113.  
  114.        
  115.        
  116.        
  117.        
  118.        
  119.         View.OnClickListener onClickListener_Post = new View.OnClickListener() {
  120.             @Override
  121.             public void onClick(View v) {
  122.                 tv_output.setText("It was clened.... By button POST");
  123.             }
  124.         };
  125.         b_post.setOnClickListener(onClickListener_Post);
  126.  
  127.  
  128.  
  129.        
  130.        
  131. //      То что должен вертуть сервер
  132. //        {
  133. //            "status": "ERR",
  134. //                "message": "Request did not contain password parameter",
  135. //                "postRequest": [],
  136. //            "getRequest": {
  137. //            "action": "testLogin",
  138. //                    "user": "[username]",
  139. //                    "passtxt": "[password]"
  140. //        }
  141. //        }
  142.  
  143.  
  144.        
  145. //  POST for -------------ERROR-------------
  146. //
  147. //            @Override
  148. //            public void onFailure(Call<ResponseBody> call, Throwable t) {
  149. //                Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
  150. //            }
  151. //  POST for -------------ERROR-------------
  152.        
  153.     }
  154.  
  155.  
  156.     @Override
  157.     public void onClick(View v) {
  158.  
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement