2015-04-28 67 views
0

我有一個項目android應用程序流視頻從視頻網址保存在數據庫中。但我不知道如何顯示視頻網址在數據庫中的視頻?請幫我.... :)如何在android中顯示來自外部數據庫(mysql)的視頻url?

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(); 
      } 
     }); 
    } 
    } 
} 

回答

0

您應該創建一個API,並使用NodeJS或PHP連接到您的MySQL數據庫。 Simple JSON for PHP會做的伎倆。

然後,您只需通過GSON library從返回的JSON中檢索url。其他幾個主題都是關於同樣的問題。 example here

相關問題