アンドロイドを使用してオープン気象APIにアクセスしようとしています。私はURLを作成する方法とhttp応答を取得する方法の2つの方法を使いました。Android HttpUrlConnection returns 500
URL
public static String API_LINK="http://samples.openweathermap.org/data/2.5/weather";
public static String apiRequest(String lat, String lng){
StringBuilder sb=new StringBuilder();
Uri builtUri = Uri.parse(API_LINK)
.buildUpon()
.appendQueryParameter("lat", lat)
.appendQueryParameter("lon", lng)
.appendQueryParameter("appid", API_KEY)
.build();
sb.append(builtUri.toString());
return sb.toString();
}
HTTPリクエスト
public String getHTTPData(String urlString){
try {
URL url=new URL(urlString);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
Log.d("Helper", httpURLConnection.getResponseCode()+"");
if(httpURLConnection.getResponseCode()==200){//ok=200
BufferedReader r=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
StringBuilder sb=new StringBuilder();
String line;
while ((line=r.readLine())!=null){
sb.append(line);
stream=sb.toString();
httpURLConnection.disconnect();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stream;
}
を作成し、私は
new GetWeather().execute(apiRequest("65.9667","-18.5333"));
MainActivity
でこのメソッドを呼び出すためにasyncTaskを使用しかし、私はミリアンペア毎回私は500のHTTPレスポンスコードを取得します。私はブラウザを使って作成したURLをナビゲートしようとしました、それは働いています。私のエミュレータではうまくいきませんでした。どうすればこの問題を解決できますか?あなたは
これは内部サーバーエラーです。サーバーの障害である可能性があります。 –
しかし、ブラウザを使用して同じURLにアクセスできます –
OpenWeatherMap APIキーを使用していませんか? –