I'm trying to access open weather api using android. I used two methods, one for creating url and one for getting http response.
Creating 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 request
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;
}
And i used a asyncTask to call this method in MainActivity
new GetWeather().execute(apiRequest("65.9667","-18.5333"));
But every time I make a request I get 500 http response code. I tried to navigate created url using browser, It's working. It not worked in my emulator.
How can i solve this. Thank you
500 Internal Server Error
Its may be
SERVER
fault .This error can only be resolved by fixes to the Web server.