php - Android:如何从 MySQL 检索数据并将其分配给变量

标签 php android mysql

如标题,我想知道如何从 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/

相关文章:

php - 使用 PHP 验证来自 login.microsoftonline.com 的 JWT 签名

PHP 用 mysql 列的总和填充数组

android - 当我们使用 SQLite 时,当我们使用 content provider 时,当我们使用 Shared preference 时

php - Yii2 在 sql 中插入日期和时间

mysql - 输出组与条件语句连接

php - 将动态表单发布到数据库

php - Laravel 5.7 登录和注册页面无法正常工作 : refreshing the appropriate pages

android - Periscope App 是如何将视频直播的如此成功?

android - android studio 中未检测到设备

用于多种条件的php sql语句正确方法