Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.Toast;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- import retrofit2.http.Body;
- import retrofit2.http.POST;
- public class MainActivity extends AppCompatActivity {
- int token = 5926166;
- class User { // request
- public String action;
- public String nickname;
- public User(String action, String nickname) {
- this.action = action;
- this.nickname = nickname;
- }
- }
- class Registration { // response
- public String status;
- public int token;
- public Registration(String status, int token) {
- this.status = status;
- this.token = token;
- }
- }
- class FetchRequest { // request
- public String action = "fetch_cards";
- public int token;
- public FetchRequest(int token) {
- this.token = token;
- }
- }
- class FetchResponse { // response
- public String status;
- public Card[] cards;
- }
- class Card {
- int count, color, shape, fill;
- @Override
- public String toString() {
- return "{ color=" + color +", count=" + count + ", shape=" + shape + ", fill=" + fill + '}';
- }
- }
- interface JsonServer {
- @POST("/")
- Call<Registration> register(@Body User user);
- @POST("/")
- Call<FetchResponse> fetch_cards(@Body FetchRequest fetchRequest);
- }
- public void onClick(View v) {
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl("http://194.176.114.21:8050")
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- JsonServer jsonServer = retrofit.create(JsonServer.class);
- String nickname = "Vasya123";
- Call<FetchResponse> call = jsonServer.fetch_cards(new FetchRequest(token));
- Callback<FetchResponse> callback = new Callback<FetchResponse>() {
- @Override
- public void onResponse(Call<FetchResponse> call, retrofit2.Response<FetchResponse> response) {
- FetchResponse fetchResponse = response.body();
- ArrayAdapter<Card> adapter = new ArrayAdapter<Card>(getApplicationContext(), R.layout.layout, fetchResponse.cards);
- ListView lv = (ListView) findViewById(R.id.cards);
- lv.setAdapter(adapter);
- }
- @Override
- public void onFailure(Call<FetchResponse> call, Throwable t) {
- }
- };
- call.enqueue(callback);
- }
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement