Android - json无法从url解析

时间:2017-04-02 19:21:42

标签: android json parsing

这是从url代码获取json,但它不起作用。 当我使用其他网址(https://api.twitch.tv/kraken/feed/erenjjing/posts?limit=50&client_id=7z9ou2blb0bfd432mcoslncc580b0c)时,它可以正常工作,但此网址(https://api.twitch.tv/kraken/channels/erenjjing/videos?broadcast_type=all&limit=20&client_id=7z9ou2blb0bfd432mcoslncc580b0c)不起作用... 这段代码中是否有错误的代码?

private String url1 = "https://api.twitch.tv/kraken/channels/";
private String url2 = "/videos?broadcast_type=all&limit=20&client_id=7z9ou2blb0bfd432mcoslncc580b0c";

private String TAG_PREVIEW = "preview";
private String TAG_TITLE = "title";
private String TAG_PUBLISHEDAT = "published_at";
private String TAG_URL = "url";
private String TAG_VIEWS = "views";
private String TAG_LENGTH = "length";

private class getFeed extends AsyncTask<String, Void, Void>
{
    private String member_name;
    public getFeed(String passedData)
    {
        member_name = passedData;
    }
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    protected Void doInBackground(String... params)
    {
        HttpHandler sh = new HttpHandler();
        String jsonStr = sh.makeServiceCall(url1 + member_name + url2);
        String strPublished;

        if (jsonStr != null) {
            try {
                JSONObject jsonObject = new JSONObject(jsonStr);
                JSONArray videos = jsonObject.getJSONArray("videos");

                for (int i = 0; i < videos.length(); i++)
                {
                    VideoData videoData = new VideoData();
                    JSONObject json_data = videos.getJSONObject(i);
                    strPublished = json_data.getString(TAG_PUBLISHEDAT);
                    String replace_strPublished = strPublished.replace("T", " ").replace("Z", " ");
                    videoData.preview = json_data.getString(TAG_PREVIEW);
                    videoData.title = json_data.getString(TAG_TITLE);
                    videoData.published = json_data.getString(replace_strPublished);
                    videoData.url = json_data.getString(TAG_URL);
                    videoData.views = json_data.getString(TAG_VIEWS);
                    videoData.length = json_data.getString(TAG_LENGTH);
                    data.add(videoData);
                }
            } catch (final JSONException e) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                    }
                });

            }
        } else {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {

                }
            });
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
        mSwipeRefreshLayout.setRefreshing(false);
        mAdapter = new AdapterTwitchVideo(getActivity(), data);
        if(mAdapter == null)
        {
            recyclerView.setAdapter(mAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        } else {
            recyclerView.setAdapter(mAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            mAdapter.notifyDataSetChanged();
        }
    }
}

0 个答案:

没有答案