0
APIを介してJSONデータを取得し、Androidに解析しようとしています。私は取得したJSONデータをログに記録しようとしていますが、 "400エラーが発生しました - 与えられた場所が無効です。" APIにアクセスするためのパラメータは正しいようですが、データを取得できない理由はわかりません。400エラー - 指定された場所が無効です
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
temperatureLabel = (TextView) findViewById(R.id.temperatureLabel);
timeLabel = (TextView) findViewById(R.id.timeLabel);
refreshButton = (ImageView) findViewById(R.id.refreshImage);
final double latitude = -104.8319;
final double longtitude = 39.7294;
refreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getWeatherForecast(latitude, longtitude);
}
});
getWeatherForecast(latitude, longtitude);
}
public void getWeatherForecast(double latitude, double longtitude) {
String apiKey = "2e31c60ad62bae1a7bba53403d1a5a40";
String forecastURL = "https://api.darksky.net/forecast/" + apiKey + "/" + latitude + ","
+ longtitude;
if (isNetworkAvailable()) {
//Build and HTTP request
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(forecastURL).build();
//Make an Api call
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
alertUserError();
}
});
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
Log.e(TAG, "JASON DATA" + jsonData);
if (response.isSuccessful()) {
mcurrentWeather = getCurrentWeatherDetails(jsonData);
// You want to update the display In the UI.
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "API call failed", Toast.LENGTH_LONG).show();
}
});
}
} catch (IOException e) {
Log.e(TAG, "Exception Caught");
} catch (JSONException e) {
Log.e(TAG, "JSONexception Caught");
}
}
});
} else {
alertUserError();
}
}
あなたは何をAPI
に送信要求と何API
があなたに送信さをチェックしようとしている場合は、ここで