return function json object

Asked

Viewed 657 times

1

i have an android function that connects to the server and returns me a json, that json and stored in a variable called object.

however I have to make this object go to another screen via Return but I’m not getting.

function

public class dados_ws {

    private static String URL;
    private static String TIPO;
    private static Map PARAMETROS;
    private static String CREDENCIAIS;

    public static JSONObject Dados_Ws(int pagina, String ParametroUrl , Context context) {

        switch (pagina) {
            //Recuperação de senha
            case 1:
                URL = Constantes_WebService.URL_REC_SENHA;
                TIPO = "GET";
                PARAMETROS = null;
                CREDENCIAIS = null + ":" + null;
                break;
        }

        WebService ws = new WebService(
                URL,
                ParametroUrl,
                TIPO,
                PARAMETROS,
                CREDENCIAIS,
                context
        );

        ws.getData(new WebService.RetornoAssincrono()

        {
            @Override
            public JSONObject onSuccess(JSONObject objeto) {
                System.out.println("Sucesso!!! Os valores são: \n" + objeto);

                return objeto;
            }
        });


        return null;
    }

}

return on the other page:

 JSONObject dadoslogin = null;
                        dadoslogin = Dados_Ws(1, ParametroUrl , context);
                        try {
                            int status = dadoslogin.getInt("status");
                            final String resultado = dadoslogin.getString("resultado");

                            System.out.println("Status = " + status);
                            System.out.println("Resultado = " + resultado);


                        } catch (JSONException e) {

                            System.out.println("Catch : " + e.getMessage());
                            //hideDialog();
                        }

this code entered directly on the home page , returns the data perfectly, but with my error function in the app and says that the variable is null.

  • can paste the error log? Where it gives null Pointer, in login data, status or result?

1 answer

1

A very good library to parse is Gson, it is a google library, https://github.com/google/gson

example to move from json to object (object name Response):

Gson gson = new Gson();
Response response = gson.fromJson("string com o json", Response.class);

example passing from object (Object name "obj") to json:

Gson gson = new Gson();
String jsonString = gson.toJson(obj);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.