如何在android中显示来自外部数据库(mysql)的视频网址?

时间:2015-04-28 07:50:34

标签: php android mysql json video

我有一个项目Android应用流视频来自视频网址保存在数据库中。但我不知道如何在数据库中显示视频网址的视频?请帮助我...... :)

public class PlayerActivity extends Activity {

private stream stream;
private ServerRequest server;
private Object newString;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);
    stream = new stream();
    server = new ServerRequest();
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) {
            newString= null;
        } else {
            newString= extras.getString("alamat");
        }
    } else {
        newString= (String) savedInstanceState.getSerializable("alamat");
    }
    final VideoView vid = (VideoView) findViewById(R.id.videoView);
    Uri video = Uri.parse(server.sendGetRequest(ServerRequest.urlSelectAll+"?id="+stream.getId().toString()));
    vid.setVideoURI(video);
    MediaController hhh = new MediaController(this);
    vid.setMediaController(hhh);
    vid.requestFocus();
    vid.start();

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

当我从列表视图中单击时,我想播放视频,这个列表视图来自我的数据库表格中有2个coloumn:视频名称和视频网址。这是列表视图的主要活动

public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private ListView listView;
private ActionMode actionMode;
private ActionMode.Callback amCallback;
private ProgressDialog progressDialog;
private ServerRequest serverRequest;
private List<stream> list;
private ListAdapterStream adapter;
private stream selectedList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    serverRequest = new ServerRequest();
    listView = (ListView) findViewById(R.id.listview_main);
    list = new ArrayList<stream>();
    /** melakukan load data melalui AsyncTask */
    new MainActivityAsync().execute("load");
}





private List<stream> processResponse(String response){
    List<stream> list = new ArrayList<stream>();
    try {
        JSONObject jsonObj = new JSONObject(response);
        JSONArray jsonArray = jsonObj.getJSONArray("stream");
        Log.d(TAG, "data lengt: "+jsonArray.length());
        stream str = null;
        for(int i = 0; i < jsonArray.length(); i++){
            JSONObject obj = jsonArray.getJSONObject(i);
            str = new stream();
            str.setId(obj.getInt("id"));
            str.setVideo(obj.getString("video"));
            str.setAlamat(obj.getString("alamat"));
            list.add(str);
        }
    } catch (JSONException e) {
        Log.d(TAG, e.getMessage());
    }
    return list;
}

private void populateListView(){
    adapter = new ListAdapterStream(getApplicationContext(), list);
    listView.setAdapter(adapter);
    listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View v, int pos, long id) {
            if(actionMode != null){
                return false;
            }
            actionMode = startActionMode(amCallback);
            v.setSelected(true);
            selectedList = (stream) adapter.getItem(pos);
            return true;
        }

    });

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View v, int pos,
                                long id) {
            selectedList = (stream) adapter.getItem(pos);
            Intent in = new Intent(getApplicationContext(), PlayerActivity.class);
            in.putExtra("id", selectedList.getId().toString());
            in.putExtra("alamat", selectedList.getAlamat());
            startActivity(in);
        }
    });



}

private class MainActivityAsync extends AsyncTask<String, Void, String>{

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("retrieving...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {

            /** Mengirimkan request ke server dan memproses JSON response */
            String response = serverRequest.sendGetRequest(ServerRequest.urlSelectAll);
            list = processResponse(response);

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        progressDialog.dismiss();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                populateListView();
            }
        });
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您应该使用连接到MySQL数据库的NodeJS或PHP创建API。 Simple JSON for PHP会做到这一点。

然后,您只需通过GSON library从返回的JSON中检索网址。其他几个主题也存在同样的问题。 example here