0

我遇到了目标 targetSdkVersion 的问题。我想从 27 移动到 29,但是当更改为 sdk 29 时,应用程序运行但不显示视频。但是当它是 sdk 27​​ 或更低时,它会显示视频。请有人帮我解决这个问题。

/**

* 一个简单的 {@link Fragment} 子类。*/ public class ChannelFragment extends Fragment { private static String GOOGLE_YOUTUBE_API_KEY = "AIzaSyBJQYpQRTzM5wuuhMUxmP7rvP3lbMGtUZ8";//这里你应该使用你的api密钥进行测试你也可以使用这个api private static String CHANNEL_ID = "UCB_ZwuWCAuB7y0B93qvnkWw"; //这里你应该使用你的频道ID进行测试你也可以使用这个api私有静态字符串CHANNLE_GET_URL =“ https://www.googleapis.com/youtube/v3/search?part=snippet&order=date&channelId= ” + CHANNEL_ID + "&maxResults=50&key=" + GOOGLE_YOUTUBE_API_KEY + "";

private RecyclerView mList_videos = null;
private VideoPostAdapter adapter = null;
private ArrayList<YoutubeDataModel> mListData = new ArrayList<>();

public ChannelFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_channel, container, false);
    mList_videos = (RecyclerView) view.findViewById(R.id.mList_videos);
    initList(mListData);
    new RequestYoutubeAPI().execute();
    return view;
}

private void initList(ArrayList<YoutubeDataModel> mListData) {
    mList_videos.setLayoutManager(new LinearLayoutManager(getActivity()));
    adapter = new VideoPostAdapter(getActivity(), mListData, new OnItemClickListener() {
        @Override
        public void onItemClick(YoutubeDataModel item) {
            YoutubeDataModel youtubeDataModel = item;
            Intent intent = new Intent(getActivity(), DetailsActivity.class);
            intent.putExtra(YoutubeDataModel.class.toString(), youtubeDataModel);
            startActivity(intent);
        }
    });
    mList_videos.setAdapter(adapter);

}


//create an asynctask to get all the data from youtube
private class RequestYoutubeAPI extends AsyncTask<Void, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();


    }


    @Override
    protected String doInBackground(Void... params) {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(CHANNLE_GET_URL);
        Log.e("URL", CHANNLE_GET_URL);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity httpEntity = response.getEntity();
            String json = EntityUtils.toString(httpEntity);
            return json;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    @Override
    protected void onPostExecute(String response) {
        super.onPostExecute(response);
        if (response != null) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                Log.e("response", jsonObject.toString());
                mListData = parseVideoListFromResponse(jsonObject);
                initList(mListData);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

public ArrayList<YoutubeDataModel> parseVideoListFromResponse(JSONObject jsonObject) {
    ArrayList<YoutubeDataModel> mList = new ArrayList<>();

    if (jsonObject.has("items")) {
        try {
            JSONArray jsonArray = jsonObject.getJSONArray("items");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = jsonArray.getJSONObject(i);
                if (json.has("id")) {
                    JSONObject jsonID = json.getJSONObject("id");
                    String video_id = "";
                    if (jsonID.has("videoId")) {
                        video_id = jsonID.getString("videoId");
                    }
                    if (jsonID.has("kind")) {
                        if (jsonID.getString("kind").equals("youtube#video")) {
                            YoutubeDataModel youtubeObject = new YoutubeDataModel();
                            JSONObject jsonSnippet = json.getJSONObject("snippet");
                            String title = jsonSnippet.getString("title");
                            String description = jsonSnippet.getString("description");
                            String publishedAt = jsonSnippet.getString("publishedAt");
                            String thumbnail = jsonSnippet.getJSONObject("thumbnails").getJSONObject("high").getString("url");

                            youtubeObject.setTitle(title);
                            youtubeObject.setDescription(description);
                            youtubeObject.setPublishedAt(publishedAt);
                            youtubeObject.setThumbnail(thumbnail);
                            youtubeObject.setVideo_id(video_id);
                            mList.add(youtubeObject);

                        }
                    }
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return mList;

}

}

4

0 回答 0