dongyi5070 2017-04-18 03:49
浏览 196

将json对象转换为字符串并显示

Basically here im using url connection to connect to php, everything is work find in log in and register phase, now i want to fetch the json object that i create in php file and then display it in a string view, so anyone got idea regarding this?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);

    dataField = (TextView) findViewById(R.id.data);
    btnDis = (Button) findViewById(R.id.btnClick);




    btnDis.setOnClickListener (new View.OnClickListener(){
        @Override
        public void onClick(View v){

            strUrl ="http://10.0.2.2/android/display.php";

            new jsonParse().execute();
        }
    });
}




public class jsonParse extends AsyncTask<String, String, String>{
    @Override
    protected void onPreExecute() {

        super.onPreExecute();

    }

    @Override
    protected void onPostExecute(String s) {

        Toast.makeText(EditActivity.this,""+result,Toast.LENGTH_LONG ).show();
    }

    @Override
    protected String doInBackground(String... params) {
        try{
            URL url = new URL (strUrl);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.connect();

            //get response from server
            BufferedReader bf = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String value = bf.readLine();
            System.out.println("result is"+value);
            result = value;

            String finalJson = bf.toString();
            JSONObject parentObject = new JSONObject(finalJson);
            JSONArray parentArray = parentObject.getJSONArray("data");

            JSONObject finalObject= parentArray.getJSONObject(0);
            String username = finalObject.getString("NAME");
            String age = finalObject.getString("AGE");
            String result = username +" - "+age;
            return result;
        }


        catch(Exception e){
            System.out.println(e);
        }

        return null;
    }
}

}

Here is my php file called display.php

<?php
require "conn.php";
require_once "global.php";



//get record from databases
$query = "SELECT * FROM user";
if($result = mysqli_query($conn, $query)){
  // printf("%d", mysqli_num_rows($result));

if(mysqli_num_rows($result) > 0){
  $status = 'true';
  $message = 'data retrieved successfully';
      while($row = mysqli_fetch_assoc($result)){
        $name = $row['name'];
        $age = $row['age'];
        $username = $row['username'];


        // echo $name."

";
        // echo $age."

";
        // echo $username."

";

        $data .= '{"NAME" : "'.$name.'", "AGE" : "'.$age.'", "USERNAME" : "'.$username.'"},';
      }

}else{
  $status = 'false';
  $message = 'data retrieved failed';
  $data = '';
}

mysqli_free_result($result);
mysqli_close($conn);
}else{
  $message = mysqli_error($conn);
}
$output = '{"status": "'.$status.'","message":"'.$message.'", "data": ['.rtrim($data, ',').']}';
echo $output;

?>
  • 写回答

4条回答 默认 最新

  • dongxi9326 2017-04-18 04:06
    关注

    You can use java library GSON https://github.com/google/gson.

    To convert json format to java String.class write:

    String str = gson.fromJson("\"abc\"", String.class);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?