【发布时间】:2020-05-10 17:55:14
【问题描述】:
我正在尝试使用 zomato API 搜索附近的餐馆并将其显示到我的应用程序中,但我得到的是空值。 Zomato API
I am using location details based on coordinates
https://developers.zomato.com/api/v2.1/geocode?lat=21.046900&lon=75.782070
当我调试应用程序时,我得到一个空响应。
这是我第一次在我的 android 应用程序中使用 API,我正在使用改造。我的目标是使用我当前的位置获取附近餐馆的列表,然后将其显示到我的应用程序中。
MainActivity
package com.example.zomatoapi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.Retrofit.Builder;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder().baseUrl(ZomatoApi.baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
ZomatoApi zomatoApi = retrofit.create(ZomatoApi.class);
Call<CurrentCity> listCall = zomatoApi.getCurrentCities();
listCall.enqueue(new Callback<CurrentCity>() {
@Override
public void onResponse(Call<CurrentCity> call, Response<CurrentCity> response) {
CurrentCity currentCity = response.body();
List<Object> objects = currentCity.getNearbyRestaurants();
for (Object object : objects)
{
Log.i("Heyy",object.toString());
}
}
@Override
public void onFailure(Call<CurrentCity> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
ZomatoApi.zava
package com.example.zomatoapi;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Headers;
public interface ZomatoApi {
String baseUrl = "https://developers.zomato.com/api/v2.1/";
@Headers("user-key: 1d63821dbb228ee5d09e8c8f7cfe10c3")
@GET("/api/v2.1/geocode?lat=21.046900&lon=75.782070")
Call<CurrentCity> getCurrentCities();
}
请帮助并为我糟糕的写作技巧感到抱歉
【问题讨论】: