AndroidManifest 

<uses-permission android:name="android.permission.INTERNET" /> -- 인터넷사용



<application

android:usesCleartextTraffic="true" -- 중요 안드로이드 http 프로토콜 접속 시 예외발생 조치

/>

 

build.gradle 

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.google.code.gson:gson:2.8.5'

 

MainActivity .

public class MainActivity extends AppCompatActivity {

    private EditText id_EditText;
    private EditText password_EditText;
    private Button LoginBtn;

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

        LoginBtn.setOnClickListener(onClickListener);
    }


    private void init(){

        id_EditText = findViewById(R.id.id_EditText);
        password_EditText = findViewById(R.id.password_EditText);
        LoginBtn = findViewById(R.id.LoginBtn);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            //데이터 ->
            if(view.equals(LoginBtn)){
                Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
                intent.putExtra("id",id_EditText.getText().toString());
                intent.putExtra("pw",password_EditText.getText().toString());
                startActivity(intent);
            }
        }
    };
}

 

 

ResultActivity

public class ResultActivity extends AppCompatActivity {

    private TextView Result_TextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        Intent intent = getIntent();
        String id = intent.getExtras().getString("id");
        String pw = intent.getExtras().getString("pw");
        init();

        RetrofitConnection connection = new RetrofitConnection();
        RetrofitInterFace retrofit = connection.getRetrofitConnection();
        retrofit.groupList(id, pw).enqueue(new Callback<Element>() {
            @Override
            public void onResponse(Call<Element> call, Response<Element> response) {

                if (response.isSuccessful()) {
                    setTextView(response.body());
                }

            }

            @Override
            public void onFailure(Call<Element> call, Throwable t) {
                Log.e("fail", t.toString());
            }
        });


    }

    private void setTextView(Element body) {
        Result_TextView.setText(body.getInfo().toString());
    }

    public void init() {
        Result_TextView = findViewById(R.id.Result_TextView);
    }


}

 

RetrofitConnection

public class RetrofitConnection {

    private static final String URL = "서버주소"; // 서버 API
    private static Retrofit retrofit = null;
    private static RetrofitInterFace retrofitInterFace = null;

    public RetrofitInterFace getRetrofitConnection() {
        if (retrofitInterFace == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(URL)
                    .addConverterFactory(GsonConverterFactory.create()) //json ->gson 
                    .build();
            retrofitInterFace = retrofit.create(RetrofitInterFace.class);
        }
        return retrofitInterFace;
    }
}

 

RetrofitInterFace

public interface RetrofitInterFace {


    @GET("/app/login")
    Call<Element> groupList(@Query("id") String id, @Query("pw") String pw);


}

 

-결과-

                                            정상적으로 데이터를 가져오는 모습입니다.

+ Recent posts