Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Cómo enviar json anidado como publicación en android

Estoy usando la biblioteca de Android Valley, actualmente solo puedo publicar json simple, tengo problemas para publicar un formato json anidado como este:

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

parece que no sabía cómo formatear el json anidado para este caso, ¿podrían ayudarme?

aquí está mi clase java que usé para conectar mi API web

 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 answers
Answer question

0

  1. Cree clases dto para el cuerpo de su solicitud:

     public class UserRequestDTO{ private UserDto user; //getters, setters } public class UserDto{ private String email; private String password; }
  1. Conviértalo a una cadena json usando Gson lib:

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

Luego conviértalo a StringEntity con el new StringEntity(stringify(new UserRequestDto(/*params*/), "UTF-8"); o a JSONObject con el new JSONObject(stringify(new UserRequestDto(/*params*/)); y utilícelo en su solicitud.

over 4 years ago · Santiago Trujillo Report

0

Si no desea la sobrecarga de Gson o la creación de clases (a menos que tenga la intención de tener objetos más complejos), simplemente puede hacer:

 JSONObject userJsonObject = new JSONObject("{ \"user\": {\"email\": \"" + email + "\", \"password\": \"" + password + "\" } }");
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!