Advertisement
Guest User

Untitled

a guest
Sep 13th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.14 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. import android.widget.Toast;
  10.  
  11. import com.google.gson.JsonObject;
  12.  
  13. import java.util.HashMap;
  14. import java.util.Map;
  15.  
  16. import cz.videotechnik.videotechnik.ApiClient;
  17. import cz.videotechnik.videotechnik.R;
  18. import cz.videotechnik.videotechnik.service.Api;
  19. import retrofit2.Call;
  20. import retrofit2.Callback;
  21. import retrofit2.Response;
  22.  
  23.  
  24. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  25.     Api api;
  26.     ApiClient ApiClient;
  27.  
  28.     private Button b_get;
  29.     private Button b_post;
  30.     private Button b_login;
  31.     private TextView tv_output;
  32.     private  EditText et_action;
  33.     private  EditText et_UserName;
  34.     private  EditText et_Password;
  35.  
  36.  
  37.     String action;
  38.     String user;
  39.     String passtxt;
  40.  
  41. //    private static final String USER_ACTION_TEST = "testLogin";
  42. //    private static final String USER_ALEXANDER_TEST = "Alexander";
  43. //    private static final String USER_PASSWORD_TEST = "123456789";
  44.  
  45.  
  46.  
  47.     @Override
  48.     protected void onCreate(Bundle savedInstanceState) {
  49.         super.onCreate(savedInstanceState);
  50.         setContentView(R.layout.activity_main);
  51.  
  52.         b_get = (Button) findViewById(R.id.b_Get);
  53.         b_post = (Button) findViewById(R.id.b_Post);
  54.         b_login = (Button) findViewById(R.id.b_login);
  55.         tv_output = (TextView) findViewById(R.id.tv_output);
  56.         et_UserName = (EditText) findViewById(R.id.et_UserName);
  57.         et_Password = (EditText) findViewById(R.id.et_Password);
  58.         et_action = (EditText) findViewById(R.id.et_action);
  59.  
  60.  
  61.         api = ApiClient.getClient().create(Api.class);
  62.  
  63. //------------------ START -----   Method POST
  64.        
  65.  
  66. //        Here we are filling "Map" which will to trnsmitted to "onClickListener_Login"
  67.         final Map<String, String> paramLogin = new HashMap<>();
  68. //        paramLogin.put("action","testLogin");
  69.         paramLogin.put("action",et_action.getText().toString());
  70.         paramLogin.put("user","Alexander");
  71.         paramLogin.put("passtxt","123456789");
  72.  
  73. //..... Code for button "LOGIN"
  74.         final View.OnClickListener onClickListener_Login = new View.OnClickListener() {
  75.             @Override
  76.             public void onClick(View v) {
  77.                 api.loginAccount(paramLogin).enqueue(new Callback<JsonObject>() {
  78.                     @Override
  79.                     public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
  80.                         tv_output.setText("Response POST:\n" + response.body().toString());
  81.                     }
  82.  
  83.                     @Override
  84.                     public void onFailure(Call<JsonObject> call, Throwable t) {
  85.                         Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
  86.                     }
  87.                 });
  88.             }
  89.         };
  90.         b_login.setOnClickListener(onClickListener_Login);
  91. //------------------ END -----   Method POST
  92.  
  93.  
  94.  
  95. //------------------ START -----   Method GET
  96. //        Here we are filling "Map" which will to trnsmitted to "onClickListener_Get"
  97.         final Map<String, String> params = new HashMap<>();
  98.         params.put("action","testRequest");
  99.         params.put("anyparam","anyvalue");
  100.  
  101. //..... Code for button "GET"
  102.         final View.OnClickListener onClickListener_Get = new View.OnClickListener() {
  103.             @Override
  104.             public void onClick(View v) {
  105.                 api.getParametrs(params).enqueue(new Callback<JsonObject>() {
  106.  
  107.                     @Override
  108.                     public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
  109.                         tv_output.setText("Response GET:\n" + response.body().toString());
  110.                     }
  111.  
  112.                     @Override
  113.                     public void onFailure(Call<JsonObject> call, Throwable t) {
  114.                         Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
  115.                     }
  116.                 });
  117.             }
  118.         };
  119.         b_get.setOnClickListener(onClickListener_Get);
  120. //------------------ End -----   Method GET
  121.  
  122.  
  123.  
  124.  
  125.  
  126. //..... Code for button "POST"
  127.         final View.OnClickListener onClickListener_Post = new View.OnClickListener() {
  128.             @Override
  129.             public void onClick(View v) {
  130.                 tv_output.setText("It was clened.... By button POST. text fromb 'RequestField'\n" + et_action.getText().toString());
  131.             }
  132.         };
  133.         b_post.setOnClickListener(onClickListener_Post);
  134. //------------------ END -----   Code for button "LOGIN"
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. //   for ------Sintaksis-------ERROR-------------
  143. //
  144. //            @Override
  145. //            public void onFailure(Call<ResponseBody> call, Throwable t) {
  146. //                Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
  147. //            }
  148. //  for -------------ERROR-------------
  149.  
  150.     }
  151.  
  152.  
  153.     @Override
  154.     public void onClick(View v) {
  155.  
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement