D
D
Denis Sokolov2021-10-10 00:30:42
Java
Denis Sokolov, 2021-10-10 00:30:42

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

1 answer(s)
C
Chesterfield25, 2021-10-10
@Chesterfield25

Because you have a local server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question