Extract JSON data within JSON that comes from a Web Service serving an Android APP

1
Good evening, how are you? I'm developing an Android APP and it consumes data from a webservice, however I'm having trouble getting some information into the APP.

This is the JSON that I return to my APP:

{
    "cod":1
    ,"dados":
        {
            "key":"123",
            "time":"123"
        }
    ,"msg":"ola"
}

As you can see, it has a main JSON and inside it has another JSON in the index% with% information of dados and cod I'm able to recover normally but the child JSON ( msg ) I can not recover . remembering that I'm using Retrofit 2 and JAVA (without any framework in the case) to do communication with the webservice. Can you give me a light? Because I have no idea what to do. Follow classes:

DatasWebService.java

import com.google.gson.annotations.SerializedName;

import org.json.JSONObject;

public class DatasWebService {

    @SerializedName("cod")
    private int cod;

    @SerializedName("msg")
    private String msg;

    @SerializedName("dados")
    private JSONObject dados;

    public int getCod() {
        return cod;
    }

    public void setCod(int cod) {
        this.cod = cod;
    }

    public String getMsg() {
        return msg;
    }

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

    public JSONObject getDados() {
        return dados;
    }

    public void setDados(JSONObject dados) {
        this.dados = dados;
    }

    @Override
    public String toString() {
        return "DatasWebService{" +
                "cod=" + cod +
                ", msg='" + msg + '\'' +
                ", dados=" + dados +
                '}';
    }
}

Data.java:

final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                  .connectTimeout(60, TimeUnit.SECONDS)
                  .readTimeout(60, TimeUnit.SECONDS)
                  .writeTimeout(60, TimeUnit.SECONDS)
                  .build();

Retrofit retrofit = new Retrofit
        .Builder()
        .baseUrl(Helper.URLAPI)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

DadosDao dDao = new DadosDao(context);

try {

    // VARIÁVEL CRIADA EM OUTRA PARTE DO ARQUIVO, NÃO TEM PROBLEMA COM ELA
    dados.put("key", dDao.getKey());

    WebService cadWebService = retrofit.create(WebService.class);

    Call<DatasWebService> call = cadWebService.datasWebService(acao, dados);

    call.enqueue(new Callback<DatasWebService>() {
        @Override
        public void onResponse(Call<DatasWebService> call, Response<DatasWebService> response) {
            DatasWebService datasWebService = response.body();

            if ((datasWebService != null) && (datasWebService.getCodStatus() == 1)) {

                try {
                    // AQUI ESTA O PROBLEMA, ELE RETORNA VAZIO MESMO O WEBSERVICE ENVIANDO O JSON, TENHO CERTEZA QUE O JSON É RETORNADO POIS COLOQUEI PRA ESCREVER NO LOG DO WEBSERVICE
                    Log.i("log", datasWebService.getDados().getString("key")); 
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } else {
                // FAZER TRATAMENTO DE RETORNO
            }
        }

        @Override
        public void onFailure(Call<DatasWebService> call, Throwable t) {

        }
    });


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

WebService.java NOT THE SERVER! IT'S AN APP INTERFACE

import org.json.JSONObject;

import io.domain.requests.DatasWebService;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface WebService {

    @FormUrlEncoded
    @POST("index.php")
    Call<DatasWebService> datasWebService(@Field("acao") String acao, @Field("dados") JSONObject dados);
}
    
asked by anonymous 18.01.2017 / 06:39

1 answer

1

Try to create a class called Data with the attributes you need to retrieve, and then replace the JSONObject attribute with it.

public class Dados {
    public String key;
    public String time;
    // criar getter e setters
 }


public class DatasWebService {

    @SerializedName("cod")
    private int cod;

    @SerializedName("msg")
    private String msg;

    @SerializedName("dados")
    private Dados dados;

    // criar getter e setters
}
    
18.01.2017 / 11:16