Answer the question
In order to leave comments, you need to log in
Can't connect to API?
Error java.io.FileNotFoundException: 10.0.2.2:64325/api/Hotels
private class GetHotels extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL("http://10.0.2.2:64325/api/Hotels");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder result = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
} catch (Exception ex) {
return ex.getMessage();
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONArray tempArray = new JSONArray(s);
for (int i = 0; i < tempArray.length(); i++) {
JSONObject hotelJson = tempArray.getJSONObject(i);
Hotel tempHotel = new Hotel(
hotelJson.getInt("Id"),
hotelJson.getString("Name"),
hotelJson.getInt("CountOfStars"),
hotelJson.getString("HotelImage")
);
mHotelsList.add(tempHotel);
mAdapter.notifyDataSetChanged();
}
} catch (Exception e) {
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question