无法从服务器获取数据进行翻新

时间:2019-03-31 20:08:40

标签: android retrofit2

我有以下代码:

function get_client_ip_server() {
  $ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
  $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
  $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
  $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
  $ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
  $ipaddress = $_SERVER['REMOTE_ADDR'];
else
  $ipaddress = 'UNKNOWN';

  return $ipaddress;
}

$ipaddress = get_client_ip_server();

function getCountry($ip){
    $curlSession = curl_init();
    curl_setopt($curlSession, CURLOPT_URL, 'http://www.geoplugin.net/json.gp?ip='.$ip);
    curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);

    $jsonData = json_decode(curl_exec($curlSession));
    curl_close($curlSession);

    return $jsonData->geoplugin_countryCode;
}

echo "County: " .getCountry($ipaddress);

此代码应从this网站获取数据。这是用于获取数据的界面(通过这种方式,我可以获得有关蝙蝠侠电影的信息):

{
  "geoplugin_request":"IP_ADDRESS",
  "geoplugin_status":200,
  "geoplugin_delay":"2ms",
  "geoplugin_credit":"Some of the returned data includes GeoLite data created by MaxMind, available from <a href='http:\/\/www.maxmind.com'>http:\/\/www.maxmind.com<\/a>.",
  "geoplugin_city":"Current City",
  "geoplugin_region":"Region",
  "geoplugin_regionCode":"Region Code",
  "geoplugin_regionName":"Region Name",
  "geoplugin_areaCode":"",
  "geoplugin_dmaCode":"650",
  "geoplugin_countryCode":"US",
  "geoplugin_countryName":"United States",
  "geoplugin_inEU":0,
  "geoplugin_euVATrate":false,
  "geoplugin_continentCode":"NA",
  "geoplugin_continentName":"North America",
  "geoplugin_latitude":"37.5563",
  "geoplugin_longitude":"-99.9413",
  "geoplugin_locationAccuracyRadius":"5",
  "geoplugin_timezone":"America\/Chicago",
  "geoplugin_currencyCode":"USD",
  "geoplugin_currencySymbol":"$",
  "geoplugin_currencySymbol_UTF8":"$",
  "geoplugin_currencyConverter":1
}

private static final String Tag = "DataUtil"; static List<Film> generateFilms(){ Log.i(Tag, "In generate films"); List <Film> films = new ArrayList<>(); String BaseUrl = "http://www.omdbapi.com/?apikey=956febbc&"; Retrofit.Builder builder = new Retrofit.Builder().baseUrl(BaseUrl) .addConverterFactory(GsonConverterFactory.create()); OkHttpClient.Builder httpClient = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS); Retrofit retrofit = builder.build(); APIService apiService = retrofit.create(APIService.class); final Call<List<Film>> filmsCall = apiService.getFilms(); filmsCall.enqueue(new Callback<List<Film>>() { @Override public void onResponse(Call<List<Film>> call, Response<List<Film>> response) { if (response.isSuccessful()){ Log.i(Tag, response.body().get(0).getTitle()); } else { Log.i(Tag, "Response code: "+response.code()); } } @Override public void onFailure(Call<List<Film>> call, Throwable t) { Log.i(getClass().getSimpleName(), "Error: "+t); } }); return films; } 行未向记录器添加任何行=>没有响应。同时,第public interface APIService { @GET("t=batman") Call<List<Film>> getFilms(); } 行也不添加任何内容。那怎么了?

UPD

当我添加Interceptor时,我在logcat中找到以下行:

Log.i(Tag, response.body().get(0).getTitle())

1 个答案:

答案 0 :(得分:1)

String BaseUrl = "http://www.omdbapi.com";

获取使用查询

@GET(".")
Call<Film> getFilms(@Query("apikey") String apikey, @Query("t") String t);

请求数据apiService.getFilms("956febbc", "batman")