1
I have an android application, and in it I keep some basic user information in Sharedpreferences, but started to show the following error parse JSON:
java.lang.Illegalstateexception: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
Codes:
Class for Manipulating Sharedpreferences:
public static void setPreferences(Context ctx, String key, Object value) {
pref = ctx.getSharedPreferences(arquivo, 0);
editor = pref.edit();
key = key.toLowerCase();
if (key == null || value == null) {
return;
}
Gson gson = new Gson();
String json = gson.toJson(value);
editor.putString(key, json);
editor.commit();
}
public static String getPreferences(Context ctx, String key) {
pref = ctx.getSharedPreferences(arquivo, 0);
String obj = pref.getString(key, "");
return obj;
}
Code to Save Something:
Note: Convert everything to Object before saving.
Gson gson = new Gson();
//Recupero o Objeto para editar o que tem gravado
String aux = SharedPreferences.getPreferences(ctx, "configuracao");
Configuracao c = gson.fromJson(aux, Configuracao.class);
//Salvo Novamente
SharedPreferences.setPreferences(ctx, "configuracao", c);
The first time saved, it works right, but when I try to get it from Sharedpreferences throws the exception above when doing the cast:
SharedPreferences.setPreferences(ctx, "configuracao", c);
Class Configuration
public class Configuracao {
private String ip;
private String porta;
private String caminhoWebService;
public Configuracao(){
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPorta() {
return porta;
}
public void setPorta(String porta) {
this.porta = porta;
}
public String getCaminhoWebService() {
return caminhoWebService;
}
public void setCaminhoWebService(String caminhoWebService) {
this.caminhoWebService = caminhoWebService;
}
}
The first time I write the configuration object and call getPreferences then the object comes this way: //Works
{"caminhoWebService":"http://192.168.254.8:8084/UltraMensagensREST/recursos"}
If I set again, replacing what was recorded, and call a get again, this comes in the return: //Casts the exception
"{\"caminhoWebService\":\"http://192.168.254.8:8084/UltraMensagensREST/recursos\"}"
In this case, it casts the exception above.
Does anyone know what it can be?
could display the Configuration Class?
– Thiago Luiz Domacoski
Opa, of course, I added the configuration class, as it had many fields, I left only the necessary ones, but it is in this format.
– Geferson
can you give me an example of wayWebService is a simple url? type: http : // www.google.com?
– Thiago Luiz Domacoski
I added how you are returning me in debug when the error happens, if you return me without these bars "" would work without problems, but why return so, have some idea?
– Geferson
I don’t understand, buddy! Without the bars it works?
– Thiago Luiz Domacoski
I edited the question, but when I try to record the second time, it seems to disfigure the json, and throws the exception above.
– Geferson