【问题标题】:How to retrieve particular row data from mysql through android Volley or JSON?如何通过 android Volley 或 JSON 从 mysql 中检索特定的行数据?
【发布时间】:2016-07-23 23:37:26
【问题描述】:

大家好,我已经使用 volley 库进行了登录和注册,并且数据已成功保存,如下面的 snap 1 所示。现在我的问题是如何在他/她登录时检索特定用户的特定或特定行数据?例如,如果用户 admin 登录如何单独获取她的整行
保存在服务器端的数据

【问题讨论】:

  • 我也遇到了同样的问题,这里是问题this

标签: android mysql json


【解决方案1】:

您将不得不使用服务器端脚本。将获取该特定数据行的 Web 服务。 该脚本应获取您的输入参数并以 Json Array 或 Object 的形式为您提供输出。

然后,您使用 volley 访问此脚本,然后您收到的响应可用于下一步。

【讨论】:

    【解决方案2】:
    <?php
        $conn = mysql_connect("localhost", "root", "");
        if(isset($conn))
        {
            mysql_select_db('your_db', $conn);
        }
        else
        {
           echo 'Sorry,can not connect to database' ;
        }
    
       $userid = isset($_GET['id']) ? mysql_real_escape_string($_GET['id']) :  "";
       $qur = mysql_query("select usename,other_fields from `your_tbl` where userid= $userid");
     $result =array();
        while($r = mysql_fetch_array($qur)){
    extract($r);
           $result[] = array("usename" => $usename,"other_fields" => $other_fields); 
    }
    $json =array("data"=>$result);
    mysql_close($conn);
    /* Output header */
    header('Content-type: application/json');
    echo json_encode($json);
    

    【讨论】:

    • 这里是 php 文件,它将为您提供包装在 json 对象中的表数据。现在您需要通过 volley get 方法捕获该 json 对象。我只是让你知道如何在 android 中处理 json 对象。这取决于您如何使用 json 对象。这只是处理 php json 和 android 的简单想法。我希望你能理解我的语言。
    【解决方案3】:

    //调用volley get方法,在success方法中获取数据

    public void onSuccess(string response) 
    {
                    try {
                            String s = new String(response);
                            JSONObject jsonObject = new JSONObject(s);
    
                           if (jsonObject.has("data")) {
                               JSONArray country =jsonObject.getJSONArray("data");
                               for (int i = 0; i < country.length(); i++) {
                                JSONObject cnt = country.getJSONObject(i);
                                String res=cnt.getString("username"));
                               //now you can use res as per your requirement
                            }
                        } catch (JSONException e) {
                       e.printStackTrace();
                    } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      相关资源
      最近更新 更多