1

I am requesting a WCF Windows Service an array of objects, and these objects contain the DateTime type. When my Android application tries to read the data from the object that is in the JSONObject message is below:

Windows Log:

11/6/2014 1:42:00 PM

Eclipse Log

Unparseable date: "/Date(1415205000000-0200)/" (at offset 0)

Android Request Method

private void GetEventos(String url)
{
    showProgressDialog();
    JsonArrayRequest req = new 
            JsonArrayRequest(url, new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response){
                    Log.d(TAG, String.valueOf(response.length()));


                    try
                    {                           
                        Evento evento;

                        for (int i = 0; i < response.length(); i++) 
                        {
                            JSONObject jsonobject = response.getJSONObject(i);                              

                            evento = new Evento();              
                            evento.setId(jsonobject.getInt("Id"));
                            evento.setNome(jsonobject.getString("Nome"));
                            evento.setIdMonitorado(jsonobject.getInt("IdMonitorado"));

                            //Test
                            SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                            Date date = dt.parse(jsonobject.getString("DataHora"));
                            Log.d(TAG, "DATA: " + date);

                            //evento.setDataHora(DateTime.parse(jsonobject.getJSONObject("DataHora")));
                            evento.setDuracao(jsonobject.getInt("Duracao"));
                            evento.setLatitudeDoLocal(Float.parseFloat(jsonobject.getString("LatitudeDoLocal")));
                            evento.setLongitudeDoLocal(Float.parseFloat(jsonobject.getString("LongitudeDoLocal")));
                            evento.setReceberNotificacao(jsonobject.getBoolean("ReceberNotificacao"));
                            evento.setNomeCidade(jsonobject.getString("NomeCidade"));
                            evento.setNomeUF(jsonobject.getString("NomeUF"));

                            _dataSoruceEvento.add(evento);

                            Log.d(TAG, evento.getDataHora().toString());
                        }

                    }catch(Exception ex){
                        Log.d(TAG, ex.getMessage().isEmpty() ? "Exception Null" : ex.getMessage());
                    }

                    hideProgressDialog();

                    //DataBindMonitorados(_dataSourceMonitorados);

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hideProgressDialog();
                }
            });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req, tag_json_arry);
}

Results when I perform the request in the browser

View Browser result

Renan Barbosa
  • 1,046
  • 3
  • 11
  • 31
  • Once you encode to JSON, you lose the entire object-ness of whatever the original was: all you get is the string representation of that object. Doesn't matter if it was a datetime object or any sort on the source machine. Now it's just text, and you'll have to tell the android code to parse whatever format that text is in. – Marc B Nov 07 '14 at 16:27

1 Answers1

0

you are gettin /Date(1415205000000-0200)/ value in the JSON and you are trying to parse it as "yyyy-MM-dd hh:mm:ss"

Aun
  • 1,883
  • 1
  • 17
  • 26
  • The value that is returned in the JSONObject is in an unknown format, when I run this same request in my browser, I get an XML and the value of the Date field is: 2014-11-06T13:42:00 I do not understand why it is not in this format / Date (1415205000000-0200) / in Json object. – Renan Barbosa Nov 07 '14 at 16:54
  • give me the web api url – Aun Nov 07 '14 at 16:59
  • Unfortunately I do not have permission to provide the url. I edited my question and added a print screen of the result of my request on the browser, notice that the date format is correct. – Renan Barbosa Nov 07 '14 at 17:18
  • Same url can give u json response by adding one header key.. Show me the json response.. Could be that json response has bug – Aun Nov 07 '14 at 17:40
  • Json response: [{"Numero":null,"IdMonitorado":2,"Notificado":false,"IdCidade":1,"NomeUF":"ACRE","LatitudeDoLocal":-1.994231E-6,"Duracao":233,"Ativo":true,"ReceberNotificacao":true,"IdUF":1,"NomeCidade":"ACRELÂNDIA","Logradouro":"Avenida Amazonas","DataHora":"\/Date(1415205000000-0200)\/","Raio":200,"Id":21,"Nome":"Evento","Bairro":null,"LongitudeDoLocal":-4.4247513E-6}, – Renan Barbosa Nov 07 '14 at 17:46
  • {"Numero":null,"IdMonitorado":2,"Notificado":false,"IdCidade":1,"NomeUF":"ACRE","LatitudeDoLocal":-2.074213E-6,"Duracao":233,"Ativo":true,"ReceberNotificacao":true,"IdUF":1,"NomeCidade":"ACRELÂNDIA","Logradouro":"Rua dos Caetes","DataHora":"\/Date(1415288520000-0200)\/","Raio":200,"Id":22,"Nome":"Teste Data","Bairro":null,"LongitudeDoLocal":-4.65966332E-6}] – Renan Barbosa Nov 07 '14 at 17:47
  • Now see... Date is not coming same as the value u got in xml.. There is formatting issue.. Contact ur server counterpart to fix it.. Issue is with server side.implementation for json response – Aun Nov 07 '14 at 17:48
  • It may be that the problem is the same web server, already checked all my Java code and is normal'll look in windows web server. Thank you. – Renan Barbosa Nov 07 '14 at 18:12