gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:29:42 25 4
gpt4 key购买 nike

我有一个项目 Android 应用程序,来自视频 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;
}

我想在从 ListView 中单击时播放视频,该 ListView 来 self 的数据库,其中我的数据库表中有 2 列:视频名称和视频 url。这是我的 ListView 的主要 Activity

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

最佳答案

您应该使用 NodeJS 或 PHP 创建一个连接到 MySQL 数据库的 API。 Simple JSON for PHP就可以了。

然后,您只需通过 GSON library 从返回的 JSON 中检索 url 。其他几个主题也涉及同一问题。 example here

关于php - 如何在android中显示来自外部数据库(mysql)的视频url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29913466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com