当前位置: 首页 > 知识库问答 >
问题:

java.lang.RuntimeException:执行doInBackground(

郭华美
2023-03-14

这是我的代码:

    protected List<MovieModel> doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            String finalJson = buffer.toString();

            JSONObject parentObject = new JSONObject(finalJson);
            JSONArray parentArray = parentObject.getJSONArray("movies");

            List<MovieModel> movieModelList = new ArrayList<>();

            Gson gson = new Gson();
            for (int i = 0; i < parentArray.length(); i++) {
                JSONObject finalObject = parentArray.getJSONObject(i);
                /**
                 * below single line of code from Gson saves you from writing the json parsing yourself
                 * which is commented below
                 */
                MovieModel movieModel = gson.fromJson(finalObject.toString(), MovieModel.class); // a single line json parsing using Gson
                movieModel.setMovie(finalObject.getString("movie"));
                movieModel.setYear(finalObject.getInt("year"));
                movieModel.setRating((float) finalObject.getDouble("rating"));
                movieModel.setDirector(finalObject.getString("director"));

                movieModel.setDuration(finalObject.getString("duration"));
                movieModel.setTagline(finalObject.getString("tagline"));
                movieModel.setImage(finalObject.getString("image"));
                movieModel.setStory(finalObject.getString("story"));

                List<MovieModel.Cast> castList = new ArrayList<>();
                for (int j = 0; j < finalObject.getJSONArray("cast").length(); j++) {
                    MovieModel.Cast cast = new MovieModel.Cast();
                    cast.setName(finalObject.getJSONArray("cast").getJSONObject(j).getString("name"));
                    castList.add(cast);
                }
                movieModel.setCastList(castList);
                // adding the final object in the list
                movieModelList.add(movieModel);
            }
            return movieModelList;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
enter code here

共有1个答案

汪才
2023-03-14

您忘记将INTERNET权限添加到您的清单中,在此

<uses-permission android:name="android.permission.INTERNET" />
 类似资料: