Hello Everone So lets Started.....
How can i get City and State name from PlaceAutocomplete api? Android ||How to Get City and State Name from Pincode in Android?
Step 1: Create a new project in Android Studio
Step 2: Add the below dependency in your build.gradle file,..
// retrofit dependencies
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
This is MY State And City Api
In Api Interface .
public interface ApiRequest {
@GET("Statelist")
@Headers({"Accept: application/json","Content-Type: application/json"})
Call<StateResponse> getStateApi(@Header("Token") String Token);
@GET("Citylist")
@Headers({"Accept: application/json","Content-Type: application/json"})
Call<CityResponse> getCityApi(@Header("Token")String cookie, @Query("state_id") String state_id);}
This is My Retrofit Request Class..
package com.daizzyinfo.food18.retrofit;
import static com.daizzyinfo.food18.utils.Constant.Token;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class RetrofitRequest {
public static Retrofit retrofit = null;
public static Retrofit getRetrofit() {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + Token)
.build();
return chain.proceed(newRequest);
}
}).build();
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.client(client)
.baseUrl("https://test-daizzyinfosys.com/food/api/")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
This is My Main Activity..
State And City Spinner....
<RelativeLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginHorizontal="15dp"
>
<Spinner
android:id="@+id/SpnState"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="14sp"
android:paddingHorizontal="15dp"
android:background="@drawable/bg_input"
>
</Spinner>
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:src="@drawable/baseline_keyboard_arrow_down_24"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
>
</ImageView>
</RelativeLayout>
<RelativeLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginHorizontal="15dp"
>
<Spinner
android:id="@+id/SpnCity"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingHorizontal="15dp"
android:textSize="14sp"
android:background="@drawable/bg_input"
>
</Spinner>
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:src="@drawable/baseline_keyboard_arrow_down_24"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
>
</ImageView>
</RelativeLayout>
in Main Activity Do This ..
Spinner SpnState,SpnCity;
ArrayAdapter stateAdapter,cityAdapter;
List<String> stateList = new ArrayList<String>();
List<String> cityList = new ArrayList<String>();
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_address);
SpnState=findViewById(R.id.SpnState);
SpnCity= findViewById(R.id.SpnCity);
Btn_Save_Address=findViewById(R.id.Btn_Save_Address);
edNumber=findViewById(R.id.edNumber);
edAddress=findViewById(R.id.edAddress);
edName=findViewById(R.id.edName);
openToolBar();
// City List
cityList.add("Select City");
cityAdapter = new ArrayAdapter(this, androidx.constraintlayout.widget.R.layout.support_simple_spinner_dropdown_item,cityList);
SpnCity.setAdapter(cityAdapter);
// State list
stateList.add("Select State");
stateAdapter = new ArrayAdapter<>(this, com.google.android.material.R.layout.support_simple_spinner_dropdown_item,stateList);
SpnState.setAdapter(stateAdapter);
stateApi();
initOnClickListener();
}
private void cityApi(String state_id) {
cityList.clear();
cityList.add("Select City");
ApiRequest apiRequest = RetrofitRequest.getRetrofit().create(ApiRequest.class);
Call<CityResponse> call = apiRequest.getCityApi(Constant.Token,state_id);
call.enqueue(new Callback<CityResponse>() {
@Override
public void onResponse(Call<CityResponse> call, Response<CityResponse> response) {
Log.e("CompleteYourProfile","On Response --"+response.body().getData().get(0));
CityResponse response1 = response.body();
if(response.code()==200){
for(int i = 0 ; i < response.body().getData().size(); i++)
{
cityList.add(response1.getData().get(i).getName());
cityAdapter.notifyDataSetChanged();
}
} else if (response.code() == 401) {
Toast.makeText(AddNewAddress.this, "On Failure ", Toast.LENGTH_SHORT).show();
} else if (response.code() == 404) {
Toast.makeText(AddNewAddress.this, " Unauthorized", Toast.LENGTH_SHORT).show();
} else if (response.code() == 500) {
Toast.makeText(AddNewAddress.this, "Internal Server Error", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AddNewAddress.this, "Data not found ", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<CityResponse> call, Throwable t) {
Toast.makeText(AddNewAddress.this, "Something Went Wrong", Toast.LENGTH_SHORT).show();
}
});
}
private void stateApi() {
stateList.clear();
stateList.add("Select State");
ApiRequest apiRequest = RetrofitRequest.getRetrofit().create(ApiRequest.class);
Call<StateResponse> call = apiRequest.getStateApi(Constant.Token);
call.enqueue(new Callback<StateResponse>() {
@Override
public void onResponse(Call<StateResponse> call, Response<StateResponse> response) {
Log.e("CompleteYourProfile", "CompleteYourProfile onResponse " + response.body().getData().get(0).getName());
Log.e("CompleteYourProfile", "CompleteYourProfile onResponse " + response.body().getData().get(0).getId());
StateResponse response1 = response.body();
if (response.code() == 200) {
for (int i = 0; i < response.body().getData().size(); i++) {
stateList.add(response.body().getData().get(i).getName());
}
} else if (response.code() == 401) {
Toast.makeText(AddNewAddress.this, "On Failure ", Toast.LENGTH_SHORT).show();
} else if (response.code() == 404) {
Toast.makeText(AddNewAddress.this, " Unauthorized", Toast.LENGTH_SHORT).show();
} else if (response.code() == 500) {
Toast.makeText(AddNewAddress.this, "Internal Server Error", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AddNewAddress.this, "Data not found ", Toast.LENGTH_SHORT).show();
}
stateAdapter.notifyDataSetChanged();
SpnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position > 0) {
String selectedStateId = String.valueOf(response.body().getData().get(position - 1).getId());
cityApi(selectedStateId);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public void onFailure(Call<StateResponse> call, Throwable t) {
Toast.makeText(AddNewAddress.this, "on Failure", Toast.LENGTH_SHORT).show();
Log.e("CompleteYourProfile", "onFailure" + t.getMessage().toString());
}
});
}
I Hope this Code is Very Helpful For Beginners ..
Thanks.
Really Apropriate
ReplyDelete