Search code examples
androidjsonresttemplate

Get ID from JSON when using REST TEMPLATE


I am using Resttemplate for my Networking. It is working quite good but i dont know how to excactly extract a specific parameter from the Server Response.

The response body looks like this :

{
    "userdata": {
        "id": "9981",
        "userid": "5a01cda4a00a3",
        "login": "[email protected]",
        "name": "yikes",
        "vorname": "yikes",
        "passwort": "3136b44e1c508d36ae4abf6f1160f779",
        "email": "[email protected]",
        "reg_date": "2017-11-07 16:13:40",

and so on.....

Edit:

try {

                    RestTemplate template = new RestTemplate();
                    final String Login = "https://app.pengueen.de//api/v1/login/?";
                    final String username1 = "username";
                    final String password1 = "password";
                    final String apikey = "api_key";


                    Uri builtUri = Uri.parse(Login)
                            .buildUpon()
                            .appendQueryParameter(username1, username.getText().toString())
                            .appendQueryParameter(password1, password.getText().toString())
                            .appendQueryParameter(apikey, "awd-DFFDD-dsse$5")
                            .build();
                   String  url = (builtUri.toString());
                       //Umwandelnb damit url conevter nichts abfuckt
                        URI uri = new URI(url);

                        //Error Handeler
                        template.setErrorHandler(new DefaultResponseErrorHandler(){
                            protected boolean hasError(HttpStatus statusCode) {
                                return false;
                            }});

                        //Response von Post bekommen
                        response = template.postForEntity(uri, null, String.class);
                        HttpStatus status = response.getStatusCode();
                        String restCall = response.getBody();






                } catch (URISyntaxException e) {


                        e.printStackTrace();

                    }
                    if (response.getStatusCode().toString().equals("200")) {

/ / Here i want to get the id so i can pass it to the next class!

                        Toast.makeText(getApplicationContext(), "Login erfolgreich", Toast.LENGTH_LONG).show();
                        Intent intent=new Intent(LoginScreen9.this,SideMenu1.class);
                        intent.putExtra("loginy","true");
                        intent.putExtra("id",response.getBody());
                        startActivity(intent);

                    }

Thank you in advance!


Solution

  • Ok, try using a JSONObject :

       String restCall = response.getBody();
       JSONObject jObject = new JSONObject(restCall); 
       JSONObject userObject = jObject.getJSONObject("userdata");
       String id = userObject.getString("id");