在焦点或onclick按钮上没有从Json解析器URL刷新播放列表

时间:2016-09-10 04:54:59

标签: android json parsing fetch onfocus

如何实现这些家伙? 我的应用程序有两个按钮。

  1. Button1从名为VideoAll.js的网址上的Json文件中获取/检索所有视频播放列表

  2. Button2从名为VideoUK.js的网址上的Json文件中获取/检索英国视频播放列表

  3. 当我点击All(VideoAll.js)按钮时,我的代码正在运行并取出播放列表,但是当我点击英国按钮时它没有刷新,我从VideoAll.js获得相同的列表我必须关闭应用程序并打开单击英国按钮首先从VideoUK.js获取正确的播放列表,但我再次无法从全部获取列表。基本上它不刷新或更新onclick甚至onfocus。它每次点击都会调用Json所以我确信这不是完成的方式而且我做错了。

    MyActivity

    public class VideoPlayerActivity extends Activity implements MediaPlayer.OnPreparedListener {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    //private Toolbar toolbar;
    private ListView mDrawerList;
    private CharSequence mDrawerTitle;
    private RelativeLayout mDrawerRelativeLayout;
    
    public static final String EXTRA_INDEX = "EXTRA_INDEX";
    public static final int PLAYLIST_ID = 6; //Arbitrary, for the example (different from audio)
    protected EMVideoView emVideoView;
    protected PlaylistManager playlistManager;
    
    protected int selectedIndex = 2;//Auto play first video
    private final String VIDEO_URL1 = "http://satdoc.dyndns.info/all.js";
    private final String VIDEO_URL2 = "http://satdoc.dyndns.info/uk.js";
    
    
    private String TAG = "VideoPlayerActivity";
    List<VideoItem> videoList = new ArrayList<>();
    private FullScreenListener fullScreenListener;
    //private Toolbar toolbar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_player_activity);
        setVolumeControlStream(3);
    
        mDrawerTitle = getTitle();
    
    
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.selection_activity_list);
        mDrawerRelativeLayout = (RelativeLayout) findViewById(R.id.left_drawer);
    
        emVideoView = (EMVideoView) findViewById(R.id.video_play_activity_video_view);
        emVideoView.setOnPreparedListener(this);
    
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            fullScreenListener = new FullScreenListener();
        }
    
        goFullscreen();
        emVideoView.setVideoViewControlsCallback(new DefaultControlsCallback());
    
        initDrawer();
    
        Button btn = (Button) findViewById(R.id.button);
        Button btn2 = (Button) findViewById(R.id.button2);
    
    
    
        btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (hasFocus) {
                    fetchVideos1();
                }
            }
    
        });
    
    
        btn2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (hasFocus) {
                    fetchVideos2();
                }
            }
    
        });
    
     }
    
    
    /**
     * Fetch for videos online.
     */
    public void fetchVideos1() {
        if (NetworkConnectionStatus.isOnline(this)) {//If there is internet connection, load reviews through a http request.
            final ProgressDialog progressDialog = new ProgressDialog(this);
    
            AsyncHttpClient client = new AsyncHttpClient();
            client.setTimeout(20_000);
            client.get(VIDEO_URL1, new AsyncHttpResponseHandler() {
    
                @Override
                public void onStart() {
                    // called before request is started
    
                }
    
                @Override
    
                public void onSuccess(int statusCode, Header[] headers, byte[] response) {// called when response HTTP status is "200 OK"
                    String jsonResponse = new String(response);
    
    
    
                    try {
    
    
                        JSONArray jsonArray = new JSONArray(jsonResponse);
                        JSONObject jsonObject;
                        String videoTitle, videoUrl;
                        int videoNum;
                        int length = jsonArray.length();
    
    
                        for (int i = 0; i < length; ++i) {
    
    
                            jsonObject = (JSONObject) jsonArray.get(i);
                            videoTitle = jsonObject.getString("chname");
                            videoUrl = jsonObject.getString("chlink");
                            videoNum = jsonObject.getInt("chid");
    
                            VideoItem videoItem = new VideoItem(videoTitle, videoUrl, videoNum);
                            videoList.add(videoItem);
                        }
    
    
                        setTitle(mDrawerTitle);
                        init();
                        mDrawerList.setAdapter(new VideoSelectionListAdapter(VideoPlayerActivity.this, videoList));
    
                    } catch (Exception e) {
                        Log.e(TAG, e.getMessage(), e);
                    }
    
                    progressDialog.dismiss();
    
                }
    
                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
    
                    Toast.makeText(VideoPlayerActivity.this, "A server error occured, Try again later", Toast.LENGTH_LONG).show();
                }
    
                @Override
                public void onRetry(int retryNo) {
                    // called when request is retried
                }
            });
    
        } else {
            Toast.makeText(VideoPlayerActivity.this, "No internet connection", Toast.LENGTH_LONG).show();
    
        }
    
    }
    
    
    
    
    public void fetchVideos2() {
        if (NetworkConnectionStatus.isOnline(this)) {//If there is internet connection, load reviews through a http request.
            final ProgressDialog progressDialog = new ProgressDialog(this);
    
    
    
            AsyncHttpClient client = new AsyncHttpClient();
            client.setTimeout(20_000);
            client.get(VIDEO_URL2, new AsyncHttpResponseHandler() {
    
                @Override
                public void onStart() {
                    // called before request is started
    
                }
    
                @Override
    
                public void onSuccess(int statusCode, Header[] headers, byte[] response) {// called when response HTTP status is "200 OK"
                    String jsonResponse = new String(response);
    
    
    
                    try {
    
    
                        JSONArray jsonArray = new JSONArray(jsonResponse);
                        JSONObject jsonObject;
                        String videoTitle, videoUrl;
                        int videoNum;
                        int length = jsonArray.length();
    
    
                        for (int i = 0; i < length; ++i) {
    
    
                            jsonObject = (JSONObject) jsonArray.get(i);
                            videoTitle = jsonObject.getString("chname");
                            videoUrl = jsonObject.getString("chlink");
                            videoNum = jsonObject.getInt("chid");
    
                            VideoItem videoItem = new VideoItem(videoTitle, videoUrl, videoNum);
                            videoList.add(videoItem);
                        }
    
    
                        setTitle(mDrawerTitle);
                        init();
                        mDrawerList.setAdapter(new VideoSelectionListAdapter(VideoPlayerActivity.this, videoList));
    
                    } catch (Exception e) {
                        Log.e(TAG, e.getMessage(), e);
                    }
    
                    progressDialog.dismiss();
    
                }
    
                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
    
                    Toast.makeText(VideoPlayerActivity.this, "A server error occured, Try again later", Toast.LENGTH_LONG).show();
                }
    
                @Override
                public void onRetry(int retryNo) {
                    // called when request is retried
                }
            });
    
        } else {
            Toast.makeText(VideoPlayerActivity.this, "No internet connection", Toast.LENGTH_LONG).show();
    
        }
    
    }
    

    这是我服务器上的第二个Json文件(VideoUK.js)

    [
    {
        "chid": 1,
        "chname": "UK1",
        "chlogo": "",
        "chlink": "http://samplevideoUK1.mp4",
        "chenable": "",
        "chnote": ""
    },
    {
        "chid": 2,
        "chname": "UK 2",
        "chlogo": "",
        "chlink": "http://samplevideoUK2.mp4",
        "chenable": "",
        "chnote": ""
    },
    {
        "chid": 3,
        "chname": "UK 3",
        "chlogo": "",
        "chlink": "http://samplevideoUK3.mp4",
        "chenable": "",
        "chnote":
    
    }
    ]
    

    这里是(VideoAll.js)

        [
    {
        "chid": 1,
        "chname": "Video 1",
        "chlogo": "",
        "chlink": "http://samplevideo1.mp4",
        "chenable": "",
        "chnote": ""
    },
    {
        "chid": 2,
        "chname": "Video 2",
        "chlogo": "",
        "chlink": "http://samplevideo2.mp4",
        "chenable": "",
        "chnote": ""
    },
    {
        "chid": 3,
        "chname": "RO 3",
        "chlogo": "",
        "chlink": "http://samplevideo3.mp4",
        "chenable": "",
        "chnote":
    }
    ]
    

    最后我的布局中的按钮

     <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="false">
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="All"
                android:id="@+id/button1"
                android:layout_weight="1" />
    
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="false" >
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/uk"
                android:id="@+id/button2"
                android:choiceMode="none"
                android:focusable="true"
                android:fadeScrollbars="false"
                android:layout_weight="1" />
    
        </LinearLayout>
    

    有人可以至少展示一个如何调用json的示例,并在每次单击刷新按钮时获取列表吗?只有首先单击的按钮才有效,但之后单击时没有任何变化。抱歉重复,但它让我发疯。

    Screenshot of my ap

1 个答案:

答案 0 :(得分:0)

首先清除按钮上的视频列表

     btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (hasFocus) {
videoList.clear();
                    fetchVideos1();
                }
            }

        });


        btn2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (hasFocus) {
videoList.clear();
                    fetchVideos2();
                }
            }

        });

并在webservices中设置适配器后通知数据集chenge:

mDrawerList.notifyDataSetChanged();

请在代码中使用此后行:

setTitle(mDrawerTitle);
                init();
                mDrawerList.setAdapter(new VideoSelectionListAdapter(VideoPlayerActivity.this, videoList));

notifyDataSetChanged();

用于刷新适配器

清除vutton上的列表