如何从mysql数据库中检索数据并在textview中解析它?

时间:2018-04-12 13:31:51

标签: android

我正在使用android编程新手,我试图从mysql数据库中获取数据并将其解析为textView,我试图获取json并且它工作正常。但是当我尝试将其解析为textView时它不起作用? 请任何帮助提供? 这是我的java方法:

protected String doInBackground(Void...params){
            InputStream is=null;
            String result="";

            try{

                URL url = new URL(json_url);
                HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
                is = new BufferedInputStream(httpURLConnection.getInputStream());

                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                StringBuilder stringBuilder = new StringBuilder();
                while((json_string = reader.readLine())!=null){

                    stringBuilder.append(json_string+"\n");
                }
                JSONObject jObject = new JSONObject(json_string);
                JSONArray jArray = jObject.getJSONArray("utilisateur");
                for (int i = 0; i < jArray.length();i++){
                    JSONObject oneObject = jArray.getJSONObject(i);
                    oneObject.getString("nom");//return the address
                    oneObject.getString("prenom");//return the name
                }


                reader.close();
                is.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();
            }catch(IOException e){
                e.printStackTrace();
            }catch (JSONException e){
                e.printStackTrace();
            }

这是我的PHP代码:

<?php

        $DB_USER='mootaz'; 
        $DB_PASS='mootaz'; 
        $DB_HOST='localhost';
        $DB_NAME='mvoting_db_1.0.0.0';


        $sql = "select * from utilisateur where role='administrateur';";

        $con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

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

        $response = array();

        while ($row = mysqli_fetch_array($result))
        {

            array_push($response,array("name"=>$row[3],"prenom"=>$row[4]));
        }

        echo json_encode(array("utilisateur"=>$response));

        mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:1)

获取存储在对象中的数据,然后将数据绑定到文本视图后,如下面的代码..

为你的回复制作pojo课程,如

public class User {
private String name,addres;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddres() {
    return addres;
}

public void setAddres(String addres) {
    this.addres = addres;
}

} 使数组列表存储如下的数据..

 private List<User> mUserList=new ArrayList<>();

当你在下面使用doInBackground方法时...

    User user=new User();
 for (int i = 0; i < jArray.length();i++){
    JSONObject oneObject = jArray.getJSONObject(i);
    user.setAddres(oneObject.getString("nom"));//return the address
    user.setName(oneObject.getString("prenom"));//return the name
    mUserList.add(user);
 }

然后在onPostExecute绑定数据之后。

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        for(User user:mUserList)
        {
            textview.setText(user.getAddres());
        }
    }