Advertisement
Kosheen

MainActivity with Retrofit example

Apr 25th, 2019
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. import android.support.v7.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. import android.util.Log;
  4. import android.view.View;
  5. import android.widget.ArrayAdapter;
  6. import android.widget.Button;
  7. import android.widget.ListView;
  8. import android.widget.Toast;
  9.  
  10. import retrofit2.Call;
  11. import retrofit2.Callback;
  12. import retrofit2.Retrofit;
  13. import retrofit2.converter.gson.GsonConverterFactory;
  14. import retrofit2.http.Body;
  15. import retrofit2.http.POST;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.     int token = 5926166;
  19.     class User { // request
  20.         public String action;
  21.         public String nickname;
  22.  
  23.         public User(String action, String nickname) {
  24.             this.action = action;
  25.             this.nickname = nickname;
  26.         }
  27.     }
  28.     class Registration { // response
  29.         public String status;
  30.         public int token;
  31.  
  32.         public Registration(String status, int token) {
  33.             this.status = status;
  34.             this.token = token;
  35.         }
  36.     }
  37.  
  38.     class FetchRequest { // request
  39.         public String action = "fetch_cards";
  40.         public int token;
  41.  
  42.         public FetchRequest(int token) {
  43.             this.token = token;
  44.         }
  45.     }
  46.     class FetchResponse { // response
  47.         public String status;
  48.         public Card[] cards;
  49.     }
  50.     class Card {
  51.         int count, color, shape, fill;
  52.  
  53.         @Override
  54.         public String toString() {
  55.             return "{ color=" + color +", count=" + count + ", shape=" + shape + ", fill=" + fill + '}';
  56.         }
  57.     }
  58.  
  59.     interface JsonServer {
  60.         @POST("/")
  61.         Call<Registration> register(@Body User user);
  62.         @POST("/")
  63.         Call<FetchResponse> fetch_cards(@Body FetchRequest fetchRequest);
  64.     }
  65.  
  66.     public void onClick(View v) {
  67.         Retrofit retrofit = new Retrofit.Builder()
  68.                 .baseUrl("http://194.176.114.21:8050")
  69.                 .addConverterFactory(GsonConverterFactory.create())
  70.                 .build();
  71.         JsonServer jsonServer = retrofit.create(JsonServer.class);
  72.  
  73.         String nickname = "Vasya123";
  74.         Call<FetchResponse> call = jsonServer.fetch_cards(new FetchRequest(token));
  75.  
  76.         Callback<FetchResponse> callback = new Callback<FetchResponse>() {
  77.             @Override
  78.             public void onResponse(Call<FetchResponse> call, retrofit2.Response<FetchResponse> response) {
  79.                 FetchResponse fetchResponse = response.body();
  80.                 ArrayAdapter<Card> adapter = new ArrayAdapter<Card>(getApplicationContext(), R.layout.layout, fetchResponse.cards);
  81.                 ListView lv = (ListView) findViewById(R.id.cards);
  82.                 lv.setAdapter(adapter);
  83.  
  84.             }
  85.  
  86.             @Override
  87.             public void onFailure(Call<FetchResponse> call, Throwable t) {
  88.  
  89.             }
  90.         };
  91.         call.enqueue(callback);
  92.     }
  93.  
  94.     protected void onCreate(Bundle savedInstanceState) {
  95.         super.onCreate(savedInstanceState);
  96.         setContentView(R.layout.activity_main);
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement