预期BEGIN_OBJECT但是在第1列第1列GSON处是STRING

时间:2016-04-13 16:47:04

标签: java json gson

查询URL并获取json对象时出现此错误。我的json:

{"status":1,"username":"admin","nombre":"Username","email":"example@example.cl","codigo_usuario":"1","hash":"2938632bfdklsngh","imagen":"05c151584cc1e9aa2985b5bd0a3a4cd2.jpeg"}

创建用户类

public class User {

private int status;
private String username;
private String nombre;
private String email;
private String codigo_usuario;
private String hash;
private String imagen;


public int getStatus() {
    return status;
}
public void setStatus(int status) {
    this.status = status;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getNombre() {
    return nombre;
}
public void setNombre(String nombre) {
    this.nombre = nombre;
}
public String getHash() {
    return hash;
}
public void setHash(String hash) {
    this.hash = hash;
}
public String getImagen() {
    return imagen;
}
public void setImagen(String imagen) {
    this.imagen = imagen;
}
public String getCodigo_usuario() {
    return codigo_usuario;
}
public void setCodigo_usuario(String codigo_usuario) {
    this.codigo_usuario = codigo_usuario;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
@Override
public String toString() {
    return "User [status=" + status + ", username=" + username
            + ", nombre=" + nombre + ", email=" + email
            + ", codigo_usuario=" + codigo_usuario + ", hash=" + hash
            + ", imagen=" + imagen + "]";
}
}

在我的课堂上,我向URL提出了这个代码的请求:

public static boolean authenticate(String username, String password){
    boolean error = false;
    User user = null;
    try {
        String request = Config.getText("URL_BASE")+Config.getText("URL_AUTHENTICATE")+username+"/"+password;
        HttpURLConnection conn = (HttpURLConnection) new URL(request).openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new Exception("Failed : HTTP error code : " + conn.getResponseCode());
        }

        user = new Gson().fromJson(new InputStreamReader(conn.getInputStream()), User.class);
        System.out.println(user.toString());
        conn.disconnect();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return error;
}

我得到了例外。我看到其他帖子有同样的错误,大多数错误是json格式或没有相同字段的类。现在,检查一下,一切都应该没问题。但由于某种原因,我收到此错误

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:741)
at controller.WService.authenticate(WService.java:29)
at view.Login._verificarLogin(Login.java:77)
at view.Login$2.actionPerformed(Login.java:44)

我是Java新手并尝试使用Web服务来获取数据和使用。我认为这是我最好的选择,但仍然不理解错误。

0 个答案:

没有答案