作为标题,我想知道如何从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"}]
答案 0 :(得分:0)
对不起,请不要回答一个真正的答案,但我还不准发表评论。
您没有&#34;数据&#34; JSON响应中的字段如果我没有弄错,那么在Android代码中调用它将会失败。所以改变这个
String data = handler.handleResponse(response);
JSONObject jObj = new JSONObject(data);
JSONObject j_user = jObj.getJSONObject("data"); // why do you do this?
String path = j_user.getString("sources");
到
String data = handler.handleResponse(response);
JSONObject jObj = new JSONObject(data);
String path = jObj.getString("sources");
我觉得你的JSON响应以[]括号开头和结尾似乎很奇怪。也许你必须首先将它转换为JSON数组并从中获取第一个JSON对象? 所有这些都应该导致Android代码出错(应该调用catch子句),但是你说你没有收到错误,所以我不确定我的想法是否可以帮到你。
修改:
你必须在try和catch之外放置路径声明或者在其中包含所有代码。还尝试首先将您的响应解析为JSON数组。试试这个:
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);
JSONArray array = new JSONArray(data);
JSONObject jObj = array.getJSONObject(0);
String path= jObj.getString("sources");
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);
}
});
} catch (Exception e) {
Log.e("Log_tag", "Error converting result" + e.toString());
}
答案 1 :(得分:0)
尝试以下代码。这应该是您确切的try... catch
块。您现在可以在path
块之外访问try
变量。
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());
}
它让我回到了你期待的道路。