Store a Json object, containing subobjects, using JSONObject Volley

0

I need to save the following object Json , emplando JSONObject (volley).

{
        "idAsignatura": {
            "idAsignatura": 1,
            "nombreAsignatura": "Cálculo"
        },
        "idEstudiante": {
            "idEstudiante": 1,
            "idUsuario": {
                "emailUsuario": "[email protected]",
                "idUsuario": 3,
                "nombreUsuario": "Victoria Martínez",
                "pwdUsuario": "1234",
                "rolUsuario": "Estudiante"
            },
            "matriculado": 1
        },
        "idTareas": 1,
        "nombreTarea": "Series de Fourier",
        "tareaNota": 7
    }

For this I use the following method:

public void onAgregaTarea (Tasks t) {

    final JSONObject jsonObjectU = new JSONObject();
    try{

        jsonObjectU.put("emailUsuario",t.getIdEstudiante().getIdUsuario().getEmailUsuario()).
                put("idUsuario",t.getIdEstudiante().getIdUsuario().getIdUsario()).
                put("nombreUsuario",t.getIdEstudiante().getIdUsuario().getNombreUsuario()).
                put("emailUsuario",t.getIdEstudiante().getIdUsuario().getEmailUsuario()).
                put("pdwUsuario",t.getIdEstudiante().getIdUsuario().getPwdUsuario()).
                put("rolUsuario",t.getIdEstudiante().getIdUsuario().getRolUsuario())  ;

    }catch(JSONException e){
        e.printStackTrace();
    }


    final JSONObject jsonObjectE = new JSONObject();
    try{

        jsonObjectU.put("idEstudiante",t.getIdEstudiante().getIdEstudiante()).
                put("idUsuario",jsonObjectU).
                put("matriculado",t.getIdEstudiante().getMatriculado()) ;

    }catch(JSONException e){
        e.printStackTrace();
    }


    final JSONObject jsonObjectA = new JSONObject();
    try{

        jsonObjectA.put("idAsignatura",t.getIdAsignatura().getIdAsignatura()).put("nombreAsignatura",t.getIdAsignatura().getNomAsiganatura());


    }catch(JSONException e){
        e.printStackTrace();
    }


    final JSONObject jsonObjectT = new JSONObject();
    try{

            jsonObjectT.put("idAsiganatura", jsonObjectA).put("idEstudiante",jsonObjectE)
                .put("nombreTarea", t.getNombreTarea()).put("tareaNota", t.getTareaNota());

    }catch(JSONException e){
        e.printStackTrace();
    }




    final RequestQueue queue = Volley.newRequestQueue(this);

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObjectT,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(ControlTareas.this, "Se envió correctamente", Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(ControlTareas.this, "Ocurrió un error al enviar la información", Toast.LENGTH_LONG).show();
        }
    });

    queue.add(request);

}

But it does not work for me ... I'm waiting for your help. Thanks.

I implemented two solutions, but both throw me the same exception, see final:

// SOLUTION1:  @Override

public void onAgregaTarea(Tareas t) {

    final JSONObject jsonObjectU = new JSONObject();
    try{

        jsonObjectU.put("emailUsuario",t.getIdEstudiante().getIdUsuario().getEmailUsuario());
        jsonObjectU.put("idUsuario",t.getIdEstudiante().getIdUsuario().getIdUsario());
        jsonObjectU.put("nombreUsuario",t.getIdEstudiante().getIdUsuario().getNombreUsuario());
        jsonObjectU.put("pwdUsuario",t.getIdEstudiante().getIdUsuario().getPwdUsuario());
        jsonObjectU.put("rolUsuario",t.getIdEstudiante().getIdUsuario().getRolUsuario()) ;

    }catch(JSONException e){
        e.printStackTrace();
    }
    JSONArray jsonArrayU = new JSONArray();
    jsonArrayU.put(jsonObjectU);



    final JSONObject jsonObjectE = new JSONObject();
    try{

        jsonObjectE.put("idEstudiante",t.getIdEstudiante().getIdEstudiante());
        jsonObjectE.put("idUsuario",jsonArrayU);
        jsonObjectE.put("matriculado",t.getIdEstudiante().getMatriculado());

    }catch(JSONException e){
        e.printStackTrace();
    }

    JSONArray jsonArrayE = new JSONArray();
    jsonArrayE.put(jsonObjectE);

    final JSONObject jsonObjectA = new JSONObject();
    try{

        jsonObjectA.put("idAsignatura",t.getIdAsignatura().getIdAsignatura());
        jsonObjectA.put("nombreAsignatura",t.getIdAsignatura().getNomAsiganatura());


    }catch(JSONException e){
        e.printStackTrace();
    }

    JSONArray jsonArrayA = new JSONArray();
    jsonArrayA.put(jsonObjectA);



    final JSONObject jsonObjectT = new JSONObject();
    try{

        jsonObjectT.put("idAsignatura", jsonArrayA);
        jsonObjectT.put("idEstudiante",jsonArrayE);
        jsonObjectT.put("nombreTarea", t.getNombreTarea());
        jsonObjectT.put("tareaNota", t.getTareaNota());

    }catch(JSONException e){
        e.printStackTrace();
    }


    final RequestQueue queue = Volley.newRequestQueue(this);

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObjectT,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(ControlTareas.this, "Registro correcto", Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(ControlTareas.this, "Ocurrió un error al enviar la información", Toast.LENGTH_LONG).show();
        }
    });

    queue.add(request);


}

}

// SOLUTION2 (Proposed by David)  @Override

public void onAgregaTarea (Tasks t) {

    // Objeto json de tipo String
    String jsonString = "{\n" +
            "        \"idAsignatura\": {\n" +
            "            \"idAsignatura\":" +t.getIdAsignatura().getIdAsignatura()+ ",\n" +
            "            \"nombreAsignatura\": \"" +t.getIdAsignatura().getNomAsiganatura()+ "\"\n" +
            "        },\n" +
            "        \"idEstudiante\": {\n" +
            "            \"idEstudiante\":" +t.getIdEstudiante().getIdEstudiante()+ ",\n" +
            "            \"idUsuario\": {\n" +
            "                \"emailUsuario\": \"" +t.getIdEstudiante().getIdUsuario().getEmailUsuario()+ "\",\n" +
            "                \"idUsuario\":" +t.getIdEstudiante().getIdUsuario().getIdUsario()+ ",\n" +
            "                \"nombreUsuario\": \"" +t.getIdEstudiante().getIdUsuario().getNombreUsuario()+ "\",\n" +
            "                \"pwdUsuario\": \"" +t.getIdEstudiante().getIdUsuario().getPwdUsuario()+ "\",\n" +
            "                \"rolUsuario\": \"" +t.getIdEstudiante().getIdUsuario().getRolUsuario()+ "\"\n" +
            "            },\n" +
            "            \"matriculado\":" +t.getIdEstudiante().getMatriculado()+ "\n" +
            "        },\n" +
            "        \"idTareas\":" +t.getIdTareas()+ ",\n" +
            "        \"nombreTarea\": \"" +t.getNombreTarea()+ "\",\n" +
            "        \"tareaNota\":" +t.getTareaNota()+ "\n" +
            "    }";


    // Objeto json de tipo JSONObject
    JSONObject jsonObjectT = null;

    try {

        jsonObjectT = new JSONObject(jsonString);

    } catch (JSONException e) {

        e.printStackTrace();
    }

    final RequestQueue queue = Volley.newRequestQueue(this);

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObjectT,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(ControlTareas.this, "Se envió correctamente", Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(ControlTareas.this, "Ocurrió un error al enviar la información", Toast.LENGTH_LONG).show();
                }
            });

    queue.add(request);


}

}

// RESULT:

// Even if you add the object to the database, it throws the following exception:

com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of                                                                          at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse (JsonObjectRequest.java:73)                                                                          at com.android.volley.NetworkDispatcher.run (NetworkDispatcher.java:123)                                                                       Caused by: org.json.JSONException: End of input at character 0 of                                                                          at org.json.JSONTokener.syntaxError (JSONTokener.java:449)                                                                          at org.json.JSONTokener.nextValue (JSONTokener.java:97)                                                                          at org.json.JSONObject. (JSONObject.java:156)                                                                          at org.json.JSONObject. (JSONObject.java:173)                                                                          at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse (JsonObjectRequest.java:68)                                                                          at com.android.volley.NetworkDispatcher.run (NetworkDispatcher.java:123)

I found the solution to this last problem: In class com.android.volley.toolbox.JsonObjectRequest ;

This method:

@Override

protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        return Response.success(new JSONObject(jsonString),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

you have to change the code to make it look like this:

@Override

protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));

        JSONObject result = null;

        if (jsonString != null && jsonString.length() > 0)
            result = new JSONObject(jsonString);

        return Response.success(result,
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

With this the problem is solved. Thanks

    
asked by HUGO MARTINEZ 11.09.2017 в 20:43
source

2 answers

0

I leave you an example of mine, adapt it to yours

public void invocacionDibujadorMovimientos(final String URLDibujadorParametro, final Context context, String  request, final Response.Listener<GenericPtsResponseMovimientos> listener, final Response.ErrorListener errorListener) {
    int method = Request.Method.POST;

    String serviceURL = URLDibujador+URLDibujadorParametro;
    ObjectMapper mapper = new ObjectMapper();
    String req = "";
    try {
        req = request;
        //RequestQueue queue = Volley.newRequestQueue(context, gethttpsUrlConnection(context, null));
        RequestQueue queue = Volley.newRequestQueue(context, null);
        objectRequest = new ObjetRequest(method, serviceURL, GenericPtsResponseMovimientos.class, req, listener, errorListener);
        objectRequest.setRetryPolicy(new DefaultRetryPolicy(timeOut, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(objectRequest);
        // } catch (JsonProcessingException e) {
        //     e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

GenericPtsResponse

@JsonIgnoreProperties(ignoreUnknown = true)
public class GenericPtsResponseMovimientos {

private int errorCode;
private String msg;
private String token;
private List<DataMovimientos> data;


public GenericPtsResponseMovimientos() {
    this.data = new ArrayList<>() ;
}

public GenericPtsResponseMovimientos(int errorCode, String msg, String token, List<DataMovimientos> data) {
    this();
    this.errorCode = errorCode;
    this.msg = msg;
    this.token = token;
    this.data = data;
}

public int getErrorCode() {
    return errorCode;
}

public void setErrorCode(int errorCode) {
    this.errorCode = errorCode;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public String getToken() {
    return token;
}

public void setToken(String token) {
    this.token = token;
}

public List<DataMovimientos> getData() {
    return data;
}

public void setData(List<DataMovimientos> data) {
    this.data = data;
}
}

DataMovement Class

public class DataMovimientos {

@JsonProperty("PaymentOrderID")
private String paymentOrderID;

@JsonProperty("ReferenciaNegocioOrd")
private String referenciaNegocioOrd;

@JsonProperty("ReferenciaCanalOrd")
private String referenciaCanalOrd;

@JsonProperty("ReferenciaNegocioBenef")
private String referenciaNegocioBenef;

@JsonProperty("ReferenciaCanalBenef")
private String referenciaCanalBenef;

@JsonProperty("BancoOrdenante")
private String bancoOrdenante;

@JsonProperty("BancoBeneficiario")
private String bancoBeneficiario;

@JsonProperty("CanalEntrada")
private String canalEntrada;

@JsonProperty("TipoCuentaOrd")
private String tipoCuentaOrd;

@JsonProperty("SucursalBancoOrd")
private String sucursalBancoOrd;

@JsonProperty("MonedaBancoOrd")
private String monedaBancoOrd;

@JsonProperty("NumeroCuentaOrd")
private String numeroCuentaOrd;

@JsonProperty("ImporteOrd")
private String importeOrd;

@JsonProperty("TipoCuentaBenef")
private String tipoCuentaBenef;

@JsonProperty("SucursalBancoBenef")
private String sucursalBancoBenef;

@JsonProperty("MonedaBancoBenef")
private String monedaBancoBenef;

@JsonProperty("NumeroCuentaBenef")
private String numeroCuentaBenef;

@JsonProperty("ImporteBenef")
private String importeBenef;

@JsonProperty("MonedaTransaccion")
private String monedaTransaccion;

@JsonProperty("ImporteTransaccion")
private String importeTransaccion;

@JsonProperty("EstadoInterno")
private String estadoInterno;

@JsonProperty("EstadoCanal")
private String estadoCanal;

@JsonProperty("Time")
private String time;
@JsonProperty("TransactionType")
private String transactionType;

@JsonProperty("Referencia")
private String referencia;


public String getTransactionType() {
    return transactionType;
}

public void setTransactionType(String transactionType) {
    this.transactionType = transactionType;
}

public DataMovimientos(String paymentOrderID, String referenciaNegocioOrd, String referenciaCanalOrd, String referenciaNegocioBenef, String referenciaCanalBenef, String bancoOrdenante, String bancoBeneficiario, String canalEntrada, String tipoCuentaOrd, String sucursalBancoOrd, String monedaBancoOrd, String numeroCuentaOrd, String importeOrd, String tipoCuentaBenef, String sucursalBancoBenef, String monedaBancoBenef, String numeroCuentaBenef, String importeBenef, String monedaTransaccion, String importeTransaccion, String estadoInterno, String estadoCanal, String time,String transactionType,String referencia) {
    this.referencia= referencia;
    this.transactionType = transactionType;
    this.paymentOrderID = paymentOrderID;
    this.referenciaNegocioOrd = referenciaNegocioOrd;
    this.referenciaCanalOrd = referenciaCanalOrd;
    this.referenciaNegocioBenef = referenciaNegocioBenef;
    this.referenciaCanalBenef = referenciaCanalBenef;
    this.bancoOrdenante = bancoOrdenante;
    this.bancoBeneficiario = bancoBeneficiario;
    this.canalEntrada = canalEntrada;
    this.tipoCuentaOrd = tipoCuentaOrd;
    this.sucursalBancoOrd = sucursalBancoOrd;
    this.monedaBancoOrd = monedaBancoOrd;
    this.numeroCuentaOrd = numeroCuentaOrd;
    this.importeOrd = importeOrd;
    this.tipoCuentaBenef = tipoCuentaBenef;
    this.sucursalBancoBenef = sucursalBancoBenef;
    this.monedaBancoBenef = monedaBancoBenef;
    this.numeroCuentaBenef = numeroCuentaBenef;
    this.importeBenef = importeBenef;
    this.monedaTransaccion = monedaTransaccion;
    this.importeTransaccion = importeTransaccion;
    this.estadoInterno = estadoInterno;
    this.estadoCanal = estadoCanal;
    this.time = time;
}
public DataMovimientos(){

}

public String getReferencia() {
    return referencia;
}

public void setReferencia(String referencia) {
    this.referencia = referencia;
}

public String getPaymentOrderID() {
    return paymentOrderID;
}

public void setPaymentOrderID(String paymentOrderID) {
    this.paymentOrderID = paymentOrderID;
}

public String getReferenciaNegocioOrd() {
    return referenciaNegocioOrd;
}

public void setReferenciaNegocioOrd(String referenciaNegocioOrd) {
    this.referenciaNegocioOrd = referenciaNegocioOrd;
}

public String getReferenciaCanalOrd() {
    return referenciaCanalOrd;
}

public void setReferenciaCanalOrd(String referenciaCanalOrd) {
    this.referenciaCanalOrd = referenciaCanalOrd;
}

public String getReferenciaNegocioBenef() {
    return referenciaNegocioBenef;
}

public void setReferenciaNegocioBenef(String referenciaNegocioBenef) {
    this.referenciaNegocioBenef = referenciaNegocioBenef;
}

public String getReferenciaCanalBenef() {
    return referenciaCanalBenef;
}

public void setReferenciaCanalBenef(String referenciaCanalBenef) {
    this.referenciaCanalBenef = referenciaCanalBenef;
}

public String getBancoOrdenante() {
    return bancoOrdenante;
}

public void setBancoOrdenante(String bancoOrdenante) {
    this.bancoOrdenante = bancoOrdenante;
}

public String getBancoBeneficiario() {
    return bancoBeneficiario;
}

public void setBancoBeneficiario(String bancoBeneficiario) {
    this.bancoBeneficiario = bancoBeneficiario;
}

public String getCanalEntrada() {
    return canalEntrada;
}

public void setCanalEntrada(String canalEntrada) {
    this.canalEntrada = canalEntrada;
}

public String getTipoCuentaOrd() {
    return tipoCuentaOrd;
}

public void setTipoCuentaOrd(String tipoCuentaOrd) {
    this.tipoCuentaOrd = tipoCuentaOrd;
}

public String getSucursalBancoOrd() {
    return sucursalBancoOrd;
}

public void setSucursalBancoOrd(String sucursalBancoOrd) {
    this.sucursalBancoOrd = sucursalBancoOrd;
}

public String getMonedaBancoOrd() {
    return monedaBancoOrd;
}

public void setMonedaBancoOrd(String monedaBancoOrd) {
    this.monedaBancoOrd = monedaBancoOrd;
}

public String getNumeroCuentaOrd() {
    return numeroCuentaOrd;
}

public void setNumeroCuentaOrd(String numeroCuentaOrd) {
    this.numeroCuentaOrd = numeroCuentaOrd;
}

public String getImporteOrd() {
    return importeOrd;
}

public void setImporteOrd(String importeOrd) {
    this.importeOrd = importeOrd;
}

public String getTipoCuentaBenef() {
    return tipoCuentaBenef;
}

public void setTipoCuentaBenef(String tipoCuentaBenef) {
    this.tipoCuentaBenef = tipoCuentaBenef;
}

public String getSucursalBancoBenef() {
    return sucursalBancoBenef;
}

public void setSucursalBancoBenef(String sucursalBancoBenef) {
    this.sucursalBancoBenef = sucursalBancoBenef;
}

public String getMonedaBancoBenef() {
    return monedaBancoBenef;
}

public void setMonedaBancoBenef(String monedaBancoBenef) {
    this.monedaBancoBenef = monedaBancoBenef;
}

public String getNumeroCuentaBenef() {
    return numeroCuentaBenef;
}

public void setNumeroCuentaBenef(String numeroCuentaBenef) {
    this.numeroCuentaBenef = numeroCuentaBenef;
}

public String getImporteBenef() {
    return importeBenef;
}

public void setImporteBenef(String importeBenef) {
    this.importeBenef = importeBenef;
}

public String getMonedaTransaccion() {
    return monedaTransaccion;
}

public void setMonedaTransaccion(String monedaTransaccion) {
    this.monedaTransaccion = monedaTransaccion;
}

public String getImporteTransaccion() {
    return importeTransaccion;
}

public void setImporteTransaccion(String importeTransaccion) {
    this.importeTransaccion = importeTransaccion;
}

public String getEstadoInterno() {
    return estadoInterno;
}

public void setEstadoInterno(String estadoInterno) {
    this.estadoInterno = estadoInterno;
}

public String getEstadoCanal() {
    return estadoCanal;
}

public void setEstadoCanal(String estadoCanal) {
    this.estadoCanal = estadoCanal;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

My Json is:

{
  "errorCode": "0",
  "msg": "",
  "token": "3ec2c554-c577-472a-993d-da4dea6d91b2",
  "data": [
    {
      "PaymentOrderID": "10527",
      "ReferenciaNegocioOrd": "",
      "ReferenciaCanalOrd": "",
      "ReferenciaNegocioBenef": "60428022-5e00-4461-b",
      "ReferenciaCanalBenef": "26916f26-5e92-49b8-8fbe-ddc59ee971c3",
      "BancoOrdenante": "10001",
      "BancoBeneficiario": "10001",
      "CanalEntrada": "MOBILE",
      "TipoCuentaOrd": "CA",
      "SucursalBancoOrd": "",
      "MonedaBancoOrd": "MXN",
      "NumeroCuentaOrd": "938584",
      "ImporteOrd": "2.000",
      "TipoCuentaBenef": "CA",
      "SucursalBancoBenef": "",
      "MonedaBancoBenef": "MXN",
      "NumeroCuentaBenef": "5435",
      "ImporteBenef": "2.000",
      "MonedaTransaccion": "MXN",
      "ImporteTransaccion": "2.00",
      "EstadoInterno": "SETTLEMENT",
      "EstadoCanal": "",
      "Time": "2017-09-11 14:48:39.050",
      "TransactionType": "DO-IT-OA-1C-D-P"
    },
    {
      "PaymentOrderID": "10526",
      "ReferenciaNegocioOrd": "",
      "ReferenciaCanalOrd": "",
      "ReferenciaNegocioBenef": "60428022-5e00-4461-b",
      "ReferenciaCanalBenef": "2591b375-437a-40bf-be42-970d3152dbf3",
      "BancoOrdenante": "10001",
      "BancoBeneficiario": "10001",
      "CanalEntrada": "MOBILE",
      "TipoCuentaOrd": "CA",
      "SucursalBancoOrd": "",
      "MonedaBancoOrd": "MXN",
      "NumeroCuentaOrd": "938584",
      "ImporteOrd": "2.000",
      "TipoCuentaBenef": "CA",
      "SucursalBancoBenef": "",
      "MonedaBancoBenef": "MXN",
      "NumeroCuentaBenef": "5435",
      "ImporteBenef": "2.000",
      "MonedaTransaccion": "MXN",
      "ImporteTransaccion": "2.00",
      "EstadoInterno": "SETTLEMENT",
      "EstadoCanal": "",
      "Time": "2017-09-11 14:48:19.767",
      "TransactionType": "DO-IT-OA-1C-D-P"
    },
    {
      "PaymentOrderID": "10524",
      "ReferenciaNegocioOrd": "",
      "ReferenciaCanalOrd": "",
      "ReferenciaNegocioBenef": "60428022-5e00-4461-b",
      "ReferenciaCanalBenef": "d9428852-1d9d-47b8-b80d-8f9e697bc0a8",
      "BancoOrdenante": "10001",
      "BancoBeneficiario": "10001",
      "CanalEntrada": "MOBILE",
      "TipoCuentaOrd": "CA",
      "SucursalBancoOrd": "",
      "MonedaBancoOrd": "MXN",
      "NumeroCuentaOrd": "100000000000009",
      "ImporteOrd": "25.000",
      "TipoCuentaBenef": "CA",
      "SucursalBancoBenef": "",
      "MonedaBancoBenef": "MXN",
      "NumeroCuentaBenef": "938584",
      "ImporteBenef": "25.000",
      "MonedaTransaccion": "MXN",
      "ImporteTransaccion": "25.00",
      "EstadoInterno": "SETTLEMENT",
      "EstadoCanal": "",
      "Time": "2017-09-11 12:34:37.793",
      "TransactionType": "WALLETTOACC.DO-LW-TA-1C-D-P"
    },
    {
      "PaymentOrderID": "10523",
      "ReferenciaNegocioOrd": "",
      "ReferenciaCanalOrd": "",
      "ReferenciaNegocioBenef": "60428022-5e00-4461-b",
      "ReferenciaCanalBenef": "b15b5c83-54cc-4a01-94d4-f13831dad2fe",
      "BancoOrdenante": "10001",
      "BancoBeneficiario": "10001",
      "CanalEntrada": "MOBILE",
      "TipoCuentaOrd": "CA",
      "SucursalBancoOrd": "",
      "MonedaBancoOrd": "MXN",
      "NumeroCuentaOrd": "100000000000009",
      "ImporteOrd": "25.000",
      "TipoCuentaBenef": "CA",
      "SucursalBancoBenef": "",
      "MonedaBancoBenef": "MXN",
      "NumeroCuentaBenef": "938584",
      "ImporteBenef": "25.000",
      "MonedaTransaccion": "MXN",
      "ImporteTransaccion": "25.00",
      "EstadoInterno": "SETTLEMENT",
      "EstadoCanal": "",
      "Time": "2017-09-11 12:34:36.630",
      "TransactionType": "WALLETTOACC.DO-LW-TA-1C-D-P"
    },
    {
      "PaymentOrderID": "10520",
      "ReferenciaNegocioOrd": "",
      "ReferenciaCanalOrd": "",
      "ReferenciaNegocioBenef": "60428022-5e00-4461-b",
      "ReferenciaCanalBenef": "4b8ceb0c-ba7f-4b95-a2d6-a3d630728a7a",
      "BancoOrdenante": "10001",
      "BancoBeneficiario": "10001",
      "CanalEntrada": "MOBILE",
      "TipoCuentaOrd": "CA",
      "SucursalBancoOrd": "",
      "MonedaBancoOrd": "MXN",
      "NumeroCuentaOrd": "938584",
      "ImporteOrd": "2.000",
      "TipoCuentaBenef": "CA",
      "SucursalBancoBenef": "",
      "MonedaBancoBenef": "MXN",
      "NumeroCuentaBenef": "5435",
      "ImporteBenef": "2.000",
      "MonedaTransaccion": "MXN",
      "ImporteTransaccion": "2.00",
      "EstadoInterno": "SETTLEMENT",
      "EstadoCanal": "",
      "Time": "2017-09-11 10:17:48.127",
      "TransactionType": "DO-IT-OA-1C-D-P"
    }
  ]
}
    
answered by 11.09.2017 в 20:47
0

It's easier if you save the json object in a String and then convert it to a JSONObject , which is what you would use in request .

To convert a json String object to a json JSONObject object is as easy as passing the json String object as a parameter to the instance of the JSONObject object.

public void onAgregaTarea(Tareas t) {

    // Objeto json de tipo String
    String jsonString = "{\n" +
            "        \"idAsignatura\": {\n" +
            "            \"idAsignatura\":" +t.getIdAsignatura().getIdAsignatura()+ ",\n" +
            "            \"nombreAsignatura\": \"" +t.getIdAsignatura().getNomAsiganatura()+ "\"\n" +
            "        },\n" +
            "        \"idEstudiante\": {\n" +
            "            \"idEstudiante\":" +t.getIdEstudiante().getIdEstudiante()+ ",\n" +
            "            \"idUsuario\": {\n" +
            "                \"emailUsuario\": \"" +t.getIdEstudiante().getIdUsuario().getEmailUsuario()+ "\",\n" +
            "                \"idUsuario\":" +t.getIdEstudiante().getIdUsuario().getIdUsario()+ ",\n" +
            "                \"nombreUsuario\": \"" +t.getIdEstudiante().getIdUsuario().getNombreUsuario()+ "\",\n" +
            "                \"pwdUsuario\": \"" +t.getIdEstudiante().getIdUsuario().getPwdUsuario()+ "\",\n" +
            "                \"rolUsuario\": \"" +t.getIdEstudiante().getIdUsuario().getRolUsuario()+ "\"\n" +
            "            },\n" +
            "            \"matriculado\":" +t.getIdEstudiante().getMatriculado()+ "\n" +
            "        },\n" +
            "        \"idTareas\":" +t.getIdTarea()+ ",\n" +
            "        \"nombreTarea\": \"" +t.getNombreTarea()+ "\",\n" +
            "        \"tareaNota\":" +t.getTareaNota()+ "\n" +
            "    }";


    // Objeto json de tipo JSONObject
    JSONObject jsonObjectT = null;

    try {

        jsonObjectT = new JSONObject(jsonString);

    } catch (JSONException e) {

        e.printStackTrace();
    }

    final RequestQueue queue = Volley.newRequestQueue(this);

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObjectT,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(ControlTareas.this, "Se envió correctamente", Toast.LENGTH_LONG).show();
                }
            }, 
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(ControlTareas.this, "Ocurrió un error al enviar la información", Toast.LENGTH_LONG).show();
            }
    });

    queue.add(request);
}
    
answered by 12.09.2017 в 02:10