выбор второго элемента в onOptionsItemSelected вызывает сбой приложения - PullRequest
0 голосов
/ 27 апреля 2018

Я пытаюсь сделать довольно простое обновление URL-адреса, который запрашивается. Я изменяю URL в onOptionsItemSelected в зависимости от того, какой пункт в меню выбран. Если я выберу первый пункт, приложение будет отлично работать. Если я выберу второй пункт, приложение вылетает. Зачем? Оба URL-адреса были проверены на правильность. Я считаю, что выбор второго элемента (наивысший рейтинг) вызывает исключение NetworkOnMainThreadException. Мы будем благодарны за любую помощь в отладке.

public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.mostPopular) {
        FILM_REQUEST_URL = "http://api.themoviedb.org/3/movie/popular?api_key=[MyNetworkKey]";
    } else if (item.getItemId() == R.id.highestRated) {
        FILM_REQUEST_URL = "http://api.themoviedb.org/3/movie/top_rated?api_key=[MyNetworkKey]";
    }

    // Clear the adapter of previous film data
    mAdapter.clear();

    // Get the revised film list
    List<Film> films = QueryUtils.fetchFilmData(FILM_REQUEST_URL);

    // Populate the adapters data set
    if (films != null && !films.isEmpty()) {
        mAdapter.addAll(films);
    }

    return super.onOptionsItemSelected(item);
}

Ошибка Logcat: Отслеживание ошибок Logcat

QueryUtils.java:

открытый класс QueryUtils {

private QueryUtils() {
}

/** Tag for log messages */
private static final String LOG_TAG = QueryUtils.class.getSimpleName();

/**
 * Query TMDb and return a list of {@link Film} objects
 */
public static List<Film> fetchFilmData(String requestUrl) {
    // Create a URL object
    URL url = createUrl(requestUrl);

    //Call HTTP request to URL and get JSON response
    String jsonResponse = null;
    try {
        jsonResponse = makeHttpRequest(url);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Problem making the HTTP request ", e);
    }

    // Extract relevant fields from the JSON response and return the list of films
    return extractFeatureFromJson(jsonResponse);
}

/**
 * Returns new URL object from the given string URL
 */
private static URL createUrl(String stringUrl) {
    URL url = null;
    try {
        url = new URL(stringUrl);
    } catch (MalformedURLException e) {
        Log.e(LOG_TAG, "Problem building the URL ", e);
    }
    return url;
}

/**
 * Make HTTP request to the URL and return a String as the response
 */
private static String makeHttpRequest(URL url) throws IOException {
    String jsonResponse = "";

    // If the URL is null, then return
    if (url == null) {
        return jsonResponse;
    }

    HttpURLConnection urlConnection = null;
    InputStream inputStream = null;
    try {
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setReadTimeout(10000 /* milliseconds */);
        urlConnection.setConnectTimeout(15000 /* milliseconds */);
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        // If the request was successful (response code 200),
        // then read the input stream and parse the response.
        if (urlConnection.getResponseCode() == 200) {
            inputStream = urlConnection.getInputStream();
            jsonResponse = readFromStream(inputStream);
        } else {
            Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
        }
    } catch (IOException e) {
        Log.e(LOG_TAG, "Problem retrieving the movie JSON results ", e);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        if (inputStream != null) {
            // Closing the input stream could throw an IOException, which is why
            // the makeHttpRequest(URL url) method signature specifies than an IOException
            // could be thrown
            inputStream.close();
        }
    }
    return jsonResponse;
}

/**
 * Convert the {@link InputStream} into a String that contains JSON
 * response from the server
 */
private static String readFromStream(InputStream inputStream) throws IOException {
    StringBuilder output = new StringBuilder();
    if (inputStream != null) {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        BufferedReader reader = new BufferedReader(inputStreamReader);
        String line = reader.readLine();
        while (line != null) {
            output.append(line);
            line = reader.readLine();
        }
    }
    return output.toString();
}

/**
 * Return a list of {@link Film} objects that has been built up from
 * parsing the given JSON response
 */
private static List<Film> extractFeatureFromJson(String filmJSON) {

    // If the JSON string is empty or null, then return
    if (TextUtils.isEmpty(filmJSON)) {
        return null;
    }

    // Create an empty ArrayList that we can start adding films to
    List<Film> films = new ArrayList<>();

    // Try to parse the JSON response string. If there's a problem with the way the JSON
    // is formatted, a JSONException exception object will be thrown
    try {

        // Create a JSONObject from the JSON response string
        JSONObject baseJsonResponse = new JSONObject(filmJSON);

        // Extract the JSONArray associated with the key called "results",
        // which represents a list of films
        JSONArray filmArray = baseJsonResponse.getJSONArray("results");

        // For each movie in the movieArray, create an {@link Film} object
        for (int i = 0; i < filmArray.length(); i++) {

            // Get a single movie at position i within the list of movies
            JSONObject currentFilm = filmArray.getJSONObject(i);

            // Extract the value for individual keys from JSONObject results
            int voteCount = currentFilm.getInt("vote_count");
            long voteAverage = currentFilm.getLong("vote_average");
            String title = currentFilm.getString("title");
            long popularity = currentFilm.getLong("popularity");
            String posterUrl = "http://image.tmdb.org/t/p/w185" + currentFilm.getString("poster_path");
            String overview = currentFilm.getString("overview");
            String releaseDate = currentFilm.getString("release_date");

            // Create a new {@link Film} object with the vote count, vote average, title,
            // popularity, poster path, overview, and release date from the JSON response
            Film film = new Film(voteCount, voteAverage, title, popularity, posterUrl,
                    overview, releaseDate);

            // Add the new {@link Film} to the list of movies
            films.add(film);
        }

    } catch (JSONException e) {
        // If an error is thrown when executing any of the above statements in the "try" block,
        // catch the exception here, so the app doesn't crash. Print a log message
        // with the message from the exception
        Log.e("QueryUtils", "Problem parsing the TMDb JSON results", e);
    }

    // Return the list of films
    return films;
}

}

1 Ответ

0 голосов
/ 27 апреля 2018

От: https://developer.android.com/reference/android/os/NetworkOnMainThreadException

NetworkOnMainThreadException:

Исключение, которое выдается, когда приложение пытается выполнить сетевую операцию в своем основном потоке.

Чтобы избежать этого исключения, вам нужно будет сделать HTTP-запрос в отдельном потоке. Один из возможных способов сделать это - использовать AsyncTask .

Здесь - сообщение о переполнении стека, в котором рассказывается, как использовать AsyncTask для выполнения HTTP-запроса GET.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...