Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
How to send nested json as post in android

im using android valley library, currently im only able to post simple json, im struggling in posting nested json format like this :

{
"user": {
    "email": "digest@example.com",
    "password": "thedigest123" 

it looks like i didnt know how to format the nested json for this case, could you help me?

here's my java class that i used to connect my web api

public void login(String email, String password) {
    String url = BASE_URL + "api/session";
    JSONObject jsonObject = new JSONObject();

    try {
        jsonObject.put("email", email);
        jsonObject.put("password", password);

        Response.Listener<JSONObject> successListener = new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Toast.makeText(mApplication, "Success", Toast.LENGTH_SHORT).show();
            }
        };

        Response.ErrorListener errorListener = new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(mApplication, "Error", Toast.LENGTH_SHORT).show();
            }
        };

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, successListener, errorListener);
        mRequestQueue.add(request);
    }

    catch (JSONException e) {
        Toast.makeText(mApplication, "JSON exception", Toast.LENGTH_SHORT).show();
    }

}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

  1. Create dto classes for your request body:

    public class UserRequestDTO{
        private UserDto user;
        //getters, setters
    }
    public class UserDto{
        private String email;
        private String password;
    }
    
  1. Convert it to json string using Gson lib:

    public static String stringify(Object obj) {
         Gson gson = new Gson();
         String jsonString = gson.toJson(obj);
         return jsonString;
    }
    

Then convert it to StringEntity with new StringEntity(stringify(new UserRequestDto(/*params*/), "UTF-8"); or to JSONObject with new JSONObject(stringify(new UserRequestDto(/*params*/));, and use it in your request.

over 4 years ago · Santiago Trujillo Denunciar

0

If you don't want the overhead of Gson or creating classes (unless you intend to have more complex objects) You can simply do:

JSONObject userJsonObject = new JSONObject("{ \"user\": {\"email\": \"" + email + "\", \"password\": \"" + password + "\" } }");
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda