Newer
Older
CosmosClient / app / src / main / java / com / example / cosmosclient / views / JoinGroupByIDActivity.java
t-tai on 4 Jul 2019 4 KB #403
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.TextView;
import android.widget.Toast;

import com.example.cosmosclient.R;
import com.example.cosmosclient.app.Cosmos;
import com.example.cosmosclient.entities.JoinGroupResponse;
import com.example.cosmosclient.resources.JoinGroupRest;

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

public class JoinGroupByIDActivity extends AppCompatActivity {
    private String GroupID=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_group_by_id);
        //テストでグループIDを出力
        final TextView test = (TextView)findViewById(R.id.IDresult);
        //buttonを取得
        Button btn = (Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //ボタンを押されると入力されたグループIDを取得
                EditText inputGroupID = (EditText)findViewById(R.id.inputGroupID);
                GroupID = inputGroupID.getText().toString();
                test.setText(GroupID);
                //retrofitの処理
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/cosmos/")
                        .addConverterFactory(JacksonConverterFactory.create())
                        .build();
                //interfaceから実装を取得
                JoinGroupRest joinGroupService = retrofit.create(JoinGroupRest.class);

                String gid="qwer";
                String uid="asdf";
                String token="sggh";
                //API呼び出しのための値入力
                Call<JoinGroupResponse> call = joinGroupService.JoinGroup(gid, uid, token);

                //サーバからデータ受け取り
                call.enqueue(new Callback<JoinGroupResponse>() {
                    //成功時
                    @Override
                    public void onResponse(Call<JoinGroupResponse> call, Response<JoinGroupResponse> response) {
                        if (response.isSuccessful()) {
//                            JoinGroupResponse result = response.body();
                            test.setText("成功");
                            Cosmos app = (Cosmos) getApplication();
//                            Intent intent = new Intent(getApplication(), GroupListActivity.class);

                            //test
//                            System.out.println(result.gId);
//                            System.out.println(result.uId);
//                            System.out.println(result.name);
//                            System.out.println(result.uri);
//                            System.out.println(result.token);
//                            System.out.println(result.iconUri);
//                            System.out.println(result.pw);

                            //ここで成功の処理を記述
//                            finish();

                        } else {
                            //onFailureでキャッチできないエラー用
                            //ここでエラーの処理を記述
                            test.setText("失敗");

                        }
                    }


                    //失敗時
                    @Override
                    public void onFailure(Call<JoinGroupResponse> call, Throwable t) {
                        t.printStackTrace();
                        Toast.makeText(JoinGroupByIDActivity.this,
                                "アカウント作成失敗",Toast.LENGTH_SHORT);
                        test.setText("超失敗");

                    }
                });
            }
        });
    }
}