duanben1909 2016-03-14 07:42
浏览 61
已采纳

通过php检索sql数据库中的一行并将输出转换为android应用程序中的textView

i am really really new to android programming so pardon me for my total lack of knowledge

as of now, i have a sql database with 3 rows, "name", "contact" and "status"

I would like to retrieve both "status" whenever that matches the query of both a "name" and "contact". After getting the "status" query,I would like to convert the query into a TextView and display it in an android activity.

I still cant find out how to do so despite hours of searching.Thank you. My biggest problem is retrieving the "status" that matches "name" and "contact". Thank you for your help

This is my php script

<?php 
 

 $name = $_GET['name'];
 $contact = $_GET['contact'];


 require_once('Connect.php');
 

 $sql = "SELECT * FROM database WHERE name=$name and contact = $contact";
 

 $r = mysqli_query($con,$sql);
 

 $row = mysqli_fetch_assoc($r);
 $status =$row["status"];
 
 echo "Your Current Status: ".$status;
 
 
 mysqli_close($con);

EDIT:

My editted snippet is this, converted it to an array somehow and it kinda works.

$result = array();
 $row = mysqli_fetch_array($r);
 array_push($result,array(

 "status"=>$row['status'],
 ));
 
 //displaying in json format 
 echo json_encode(array('result'=>$result));

However, I have totally no idea how to use JSON to retrieve the status from the array and convert it to a String (and eventually TextView). Thank you,anyone for helping

</div>
  • 写回答

2条回答 默认 最新

  • dqkv0603 2016-03-14 07:57
    关注

    Try

      $sql = "SELECT * FROM your_table WHERE name='$name' and contact = '$contact' ";
    

    mind the single quote ' ' around your variables.

    Use mysqli_error() function to find any errors.

    Further make sure you use JSON to communicate with your server and the android client app.

    For that you should have a Class called "JsonParser" insert below method.

        public JSONObject makeHttpRequest(String url, List<NameValuePair> params) {
            try {
    
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
    
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
    
                is = httpEntity.getContent();
    
            }catch(UnsupportedEncodingException e){
                Log.e(tag + "-> EncodingError", e.toString());
                e.printStackTrace();
            }catch(ClientProtocolException e){
                Log.e(tag + "-> ProtocolError", e.toString());
                e.printStackTrace();
            }catch(IOException e){
                Log.e(tag + "-> IOError", e.toString());
                e.printStackTrace();
        }
    
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
    
            String line = null;
    
            while((line = br.readLine())!= null){
                sb.append(line + "
    ");
            }
            is.close();
            json = sb.toString();
            Log.i(tag, "-> JSON String Buffer Created");
    
        }catch(Exception e){
            Log.e(tag + "-> Buffer Error", "Conversion Error" + e.toString());
        }
    
        try{
            Log.i("tagconvertstr", "["+json+"]");
            jobj = new JSONObject(json);
            Log.i(tag, "-> JSON object Created");
    
        }catch(JSONException e){
            Log.e(tag + "-> JSON Parser", e.toString());
        }
    
        return jobj;
    }
    

    Next initialize your Textview

        private Textview txtviw;
    
        JSONObject jobj = null;
        JsonParser jParser = new JsonParser();
    

    In onCreate()

        txtviw = (Textview ) findViewById(R.id.your_text_view_id);
    

    Then in an AsyncTask class call this php file and get the json object created.

    In doInBackground()

            List<NameValuePair> params = new ArrayList<NameValuePair>();
    
            params.add(new BasicNameValuePair("name", your_name));
            params.add(new BasicNameValuePair("contact", your_contact));
    
            jobj = jParser.makeHttpRequest(YOUR_URL_TO_PHP_FILE, params);
    

    finally in onPostExecute()

        txtviw.setText(jobj.getString("status"));
    

    You can learn more about AsyncTask from google. Further it is a best practice to use AsyncTask wen communicating with databases.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据