-4

I am trying to display information retrieved from server using php in an android app,I have retrieved the information in a String with json formatting here is the retrieve code and php code. PHP code

<?php 
 if($_SERVER['REQUEST_METHOD']=='POST')
{

    //Getting values
     $username = $_POST['username'];
     require_once('dbConnect.php');

     $sql = "SELECT * FROM `employee` WHERE username='$username'";
     $result=mysqli_query($con,$sql);
     $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
     echo json_encode($row);
}

Android Retrieve code

 @Override
    protected String doInBackground(Void... params) {
        // TODO: attempt authentication against a network service.

        HashMap<String,String> params1= new HashMap<>();
        params1.put(Config.KEY_EMP_UN,mUsername);

        RequestHandler rh = new RequestHandler();
        String res = rh.sendPostRequest(Config.URl_RET, params1);
        return res;
        // TODO: register the new account here.
    }

    @Override
    protected void onPostExecute(final String success) {
        Toast.makeText(Main2Activity.this,success,Toast.LENGTH_LONG).show();
        final TextView Textview1 = (TextView)findViewById(R.id.textView);
        Textview1.setText(success);
    }

This retrieves info in format shown in below imageenter image description here

What is want to do is extract Name,Designation,Salary,Password and display them in separate TextView.How can I do that?

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
Tanmay Kajbaje
  • 61
  • 1
  • 3
  • 9

3 Answers3

1

Take your server response and parse it like this

try {
JSONObject jsonObject = new JSONObject(response);

if (jsonObject.has("name")) {
String name=jsonObject.getString("name");
}
if (jsonObject.has("designation")) {
String designation=jsonObject.getString("designation");
}
if (jsonObject.has("salary")) {
int salary=jsonObject.getInt("salary");
}
if(jsonObject.has("password")){
String password=jsonObject.getString("password");
}
}catch (Exception e) {

}
Omar Hayat
  • 378
  • 2
  • 12
0

you have to parse the json :

JSONObject jsonObject=new JSONObject(success);
//key value for eg : name,salary etc
// now do whatever you want to do
jsonObject.getString("your key value");
Dhruv Tyagi
  • 814
  • 1
  • 9
  • 28
0

There are lots of possible duplicates to this question, so I recommend closing this question after reading this answer.

What you have in here is a JSON response and in order to parse information in JSON, you have to use a JSON parser. Libraries like Gson, Jackson, Moshi will be able to do this.

If you're using Gson, you have to generate model classes of the response which can be written manually or auto-generated using jsonschema2pojo. Once Gson parses your JSON, you can use your model classes getters to access data.

Links to tutorials

https://kylewbanks.com/blog/Tutorial-Android-Parsing-JSON-with-GSON

Youtube video

https://www.youtube.com/watch?v=y96VcLgOJqA

capt.swag
  • 10,335
  • 2
  • 41
  • 41