Newer
Older
CosmosClient / app / src / main / java / com / example / cosmosclient / views / MakeGroupActivity.java
package com.example.cosmosclient.views;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.cosmosclient.R;
import com.example.cosmosclient.app.Cosmos;
import com.example.cosmosclient.entities.Group;
import com.example.cosmosclient.entities.SignupResponse;
import com.example.cosmosclient.entities.User;
import com.example.cosmosclient.resources.GroupsRest;
import com.example.cosmosclient.resources.UsersRest;
import com.example.cosmosclient.retrofitsample.Ele;

import java.io.IOError;
import java.io.IOException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class MakeGroupActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_group);

        final EditText gruopNameText = findViewById(R.id.groupNameText);
        Button createGroupButton = findViewById(R.id.createGroupButton);
        String name;
        final String uId,token;

        //グループ作成に必要な情報の取得
        Cosmos app = (Cosmos) getApplication();
        uId = app.getuId();
        //token = app.getToken();
        token="abcdef";

        //retrofitの処理
        final Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/cosmos/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        //interfaceから実装を取得
        final GroupsRest makeGroupService = retrofit.create(GroupsRest.class);

        createGroupButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                try {
                    Call<Group> createGroup = makeGroupService.createGroup(gruopNameText.getText().toString(), uId, token);

                    createGroup.enqueue(new Callback<Group>() {
                        @Override
                        public void onResponse(Call<Group> call, Response<Group> response) {
                            if (response.isSuccessful()) {
                                Group result = response.body();

                                System.out.println(result.getgId());
                                System.out.println(result.getName());
                                System.out.println(result.getRequests());
                                System.out.println(result.getUri());
                                System.out.println(result.getgId());

                                Toast.makeText(MakeGroupActivity.this, "グループを作成しました", Toast.LENGTH_SHORT).show();

                                finish();

                            } else {
                                System.out.println("");
                            }
                        }

                        @Override
                        public void onFailure(Call<Group> call, Throwable t) {
                            t.printStackTrace();
                        }
                    });
                }catch(Exception e){
                    e.printStackTrace();
                }

            }
        });








    }
}