From 0932e15121e43601dc065671df00723c5dce75cf Mon Sep 17 00:00:00 2001 From: hhj Date: Sun, 11 Jun 2023 02:03:14 +0900 Subject: [PATCH] =?UTF-8?q?[ADD]=20api=20=EB=AF=B8=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/nbit/Idear/MainActivity.kt | 14 +++ .../java/com/nbit/Idear/home/ApiService.kt | 105 ++++++++++++++++++ .../com/nbit/Idear/home/RetrofitInterface.kt | 35 ++++++ 3 files changed, 154 insertions(+) create mode 100644 app/src/main/java/com/nbit/Idear/home/ApiService.kt create mode 100644 app/src/main/java/com/nbit/Idear/home/RetrofitInterface.kt diff --git a/app/src/main/java/com/nbit/Idear/MainActivity.kt b/app/src/main/java/com/nbit/Idear/MainActivity.kt index 4c847a7..7cd30ad 100644 --- a/app/src/main/java/com/nbit/Idear/MainActivity.kt +++ b/app/src/main/java/com/nbit/Idear/MainActivity.kt @@ -5,6 +5,8 @@ import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.core.view.GravityCompat import androidx.recyclerview.widget.LinearLayoutManager +import com.footstep.dangbal.kotlin.src.main.map.ApiService +import com.footstep.dangbal.kotlin.src.main.map.RetrofitInterface import com.nbit.Idear.databinding.ActivityMainBinding import com.nbit.Idear.databinding.MainIncludeDrawerBinding import com.nbit.Idear.home.ProxyWriteAdapter @@ -12,6 +14,8 @@ import com.nbit.Idear.home.ProxyWriteData import com.nbit.Idear.home.WriteSubData import com.nbit.Idear.mypage.MyPageActivity import com.nbit.Idear.write.WriteActivity +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory // 메인 페이지 class MainActivity : AppCompatActivity() { @@ -107,5 +111,15 @@ class MainActivity : AppCompatActivity() { val intent = Intent(this, WriteActivity::class.java) startActivity(intent) } + + //val retrofit = Retrofit.Builder() + // .baseUrl("https://api.example.com/") // 서버의 baseUrl을 설정합니다. + // .addConverterFactory(GsonConverterFactory.create()) // JSON 데이터 변환을 위한 Gson 변환기를 추가합니다. + // .build() + + // val apiService = retrofit.create(RetrofitInterface::class.java) + val apiService = RetrofitInterface.create() + + } } \ No newline at end of file diff --git a/app/src/main/java/com/nbit/Idear/home/ApiService.kt b/app/src/main/java/com/nbit/Idear/home/ApiService.kt new file mode 100644 index 0000000..5c95e36 --- /dev/null +++ b/app/src/main/java/com/nbit/Idear/home/ApiService.kt @@ -0,0 +1,105 @@ +package com.footstep.dangbal.kotlin.src.main.map + +import android.util.Log +import com.naver.maps.map.overlay.Marker +import com.footstep.dangbal.kotlin.config.ApplicationClass +import com.footstep.dangbal.kotlin.src.main.map.model.AllResponse +import com.footstep.dangbal.kotlin.src.main.map.model.CityResponse +import com.footstep.dangbal.kotlin.src.main.map.model.PopupResponse +import com.footstep.dangbal.kotlin.src.main.map.model.SpecificFstResponse + +class ApiService(val mapFragmentInterface: MapFragment) { +//val accessToken="Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImZvb3RzdGVwQG5hdmVyLmNvbSIsImlhdCI6MTY3NDkxNDc2NiwiZXhwIjoxNjc1MjE3MTY2fQ.KxwX1Q0o-omU1rRIiUJBd9gLPbTRVciP_9g_sklW1Bk" + suspend fun tryGetMapFootStepList(){ + var return_map=HashMap() +// ApplicationClass.sSharedPreferences.edit().putString(X_ACCESS_TOKEN, accessToken).apply() + + val mapRetrofitInterface=ApplicationClass.sRetrofit.create(RetrofitInterface::class.java) + + try{ + val response=mapRetrofitInterface.getMapFootStepList() + Log.d("FootStepList", "tryGetMapFootStepList 되긴하니?") + + if(response!=null) + mapFragmentInterface.onGetMapFootStepListSuccess(response as AllResponse) + else + Log.d("FootStepList", "맵 서비스 List에서의 결과 : ${response.toString()}") + } + catch (e: Exception) { + Log.d("FootStepList", "onGetMapFootStepListFailure "+e.message.toString()) + + mapFragmentInterface.onGetMapFootStepListFailure(e.message ?: "통신 오류") + } + } + + suspend fun tryGetMapFootStepPopup(place_id:Int){ + // Log.d("FootStepList", "tryGetMapFootStepPopup 진입") + + // ApplicationClass.sSharedPreferences.edit().putString(X_ACCESS_TOKEN, accessToken).apply() + + val mapRetrofitInterface=ApplicationClass.sRetrofit.create(RetrofitInterface::class.java) + Log.d("FootStepList", "tryGetMapFootStepPopup 안쪽직전진입") + + try{ + val response=mapRetrofitInterface.getMapFootStepPopup(place_id) + Log.d("FootStepList", "tryGetMapFootStepPopup 되긴하니?") + + if(response!=null) { + mapFragmentInterface.onGetMapFootStepPopupSuccess( + response as PopupResponse, + place_id + ) + } + else + Log.d("FootStepList", "맵 서비스 Popup에서의 결과 : ${response.toString()}") + } + catch (e: Exception) { + Log.d("FootStepList", "onGetMapFootStepPopupFailure api"+e.message.toString()) + + mapFragmentInterface.onGetMapFootStepPopupFailure(e.message ?: "통신 오류") + } + } + + suspend fun tryGetMapFootStepSpecific(start_date :String,ene_date:String){ + val mapRetrofitInterface=ApplicationClass.sRetrofit.create(RetrofitInterface::class.java) + Log.d("FootStepList2", "tryGetMapFootStepSpecific 안쪽직전진입") + + try{ + val response=mapRetrofitInterface.getMapFootStepSpecific(start_date,ene_date) + Log.d("FootStepList", "tryGetMapFootStepPopup 되긴하니?") + + if(response!=null) { + mapFragmentInterface.onGetMapFootStepSpecificSuccess(response as SpecificFstResponse) + } + else + Log.d("FootStepList", "맵 서비스 Specific에서의 결과 : ${response.toString()}") + } + catch (e: Exception) { + Log.d("FootStepList", "onGetMapFootStepSpecificFailure"+e.message.toString()) + + mapFragmentInterface.onGetMapFootStepSpecificFailure(e.message ?: "통신 오류") + } + } + + suspend fun tryGetMapFootStepCity(city :String){ +// ApplicationClass.sSharedPreferences.edit().putString(X_ACCESS_TOKEN, accessToken).apply() + + val mapRetrofitInterface=ApplicationClass.sRetrofit.create(RetrofitInterface::class.java) + + try{ + val response=mapRetrofitInterface.getMapFootStepCity(city) + Log.d("FootStepList", "tryGetMapFootStepCity 되긴하니?") + + if(response!=null) + mapFragmentInterface.onGetMapFootStepCitySuccess(response as CityResponse) + else + Log.d("FootStepList", "맵 서비스 City에서의 결과 : ${response.toString()}") + } + catch (e: Exception) { + Log.d("FootStepList", "tryGetMapFootStepCity onGetMapFootStepListFailure "+e.message.toString()) + + mapFragmentInterface.onGetMapFootStepCityFailure(e.message ?: "통신 오류") + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/nbit/Idear/home/RetrofitInterface.kt b/app/src/main/java/com/nbit/Idear/home/RetrofitInterface.kt new file mode 100644 index 0000000..93319be --- /dev/null +++ b/app/src/main/java/com/nbit/Idear/home/RetrofitInterface.kt @@ -0,0 +1,35 @@ +package com.footstep.dangbal.kotlin.src.main.map + +import com.footstep.dangbal.kotlin.src.main.map.model.* +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import retrofit2.http.* + +interface RetrofitInterface { + + @GET("/footstep/all") + suspend fun getMapFootStepList() : AllResponse + + @GET("/footstep/{place-id}") + suspend fun getMapFootStepPopup(@Path("place-id") place_id : Int) : PopupResponse + + @GET("/footstep/specific/{start-date}/{end-date}") + suspend fun getMapFootStepSpecific(@Path("start-date") start_date : String, @Path("end-date") end_date:String) : SpecificFstResponse + + @GET("/footstep/city/{city-name}") + suspend fun getMapFootStepCity(@Path("city-name")city: String) : CityResponse + + @GET("users/{user}") + fun getUser(@Path("user") username: String): Call + + companion object { + fun create(): ApiService { + val retrofit = Retrofit.Builder() + .baseUrl("https://api.example.com/") + .addConverterFactory(GsonConverterFactory.create()) + .build() + + return retrofit.create(ApiService::class.java) + } + } +}