如标题,我想知道如何从 MySQL 数据库检索单个变量并分配给变量。
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://192.168.1.25/userdatabase/include/GetSource.php");
HttpResponse response = httpClient.execute(httpPost);
BasicResponseHandler handler = new BasicResponseHandler();
String data = handler.handleResponse(response);
JSONObject jObj = new JSONObject(data);
String path= jObj.getString("sources");
}catch (Exception e) {
Log.e("Log_tag", "Error converting result" + e.toString());
}
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(getActivity()));
mVideoView.setVideoChroma(MediaPlayer.VIDEOCHROMA_RGB565);
mVideoView.requestFocus();
progressBar.setVisibility(View.VISIBLE);
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// optional need Vitamio 4.0
progressBar.setVisibility(View.GONE);
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
这是我尝试从 MySQL 检索路径的方法。但未能检索。 请帮忙
<小时/><?php
header('Content-type: application/json; charset=UTF-8');
mysql_connect("127.0.0.1","root","");
mysql_select_db("streaming");
$sql=mysql_query("select * from iptv where id = 1");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
json_encode($output);
print(json_encode($output, JSON_UNESCAPED_SLASHES));
mysql_close();
?>
<小时/>
[{"sources":"http://210.200.79.25:1935/live/ttv.sdp/chunklist.m3u8"}]
最佳答案
尝试下面的代码。这应该是您确切的 try...catch
block 。您现在可以在 try
block 外部访问 path
变量。
String path = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://192.168.1.25/userdatabase/include/GetSource.php");
HttpResponse response = httpClient.execute(httpPost);
BasicResponseHandler handler = new BasicResponseHandler();
String data = handler.handleResponse(response).replace('"','\''); //edited this line
JSONArray ja = new JSONArray(data); //added this line
JSONObject jObj = ja.getJSONObject(0); //edited this line
path= jObj.getString("sources");
} catch (Exception e) {
Log.e("Log_tag", "Error converting result" + e.toString());
}
它返回了我您期望的路径。
关于php - Android:如何从 MySQL 检索数据并将其分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29645028/