【问题标题】:Failed to load.com.google.gson.JsonSyntaxException | Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $加载失败。com.google.gson.JsonSyntaxException |预期为 BEGIN_OBJECT,但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY
【发布时间】:2018-09-19 14:33:10
【问题描述】:

我正在尝试将数据显示到TextViews。我是 Retrofit 的新手,希望能提供任何帮助和提示。以下是我目前所拥有的。

API 客户端:

public class ApiClient {
    public static final String BASE_URL = ("http://10.1.11.11/");
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

API 服务:

public interface ApiService {
    @FormUrlEncoded
    @GET("/lara/getprofile.php?id=")
    Call<Profile> getMyProfile(@Query("id") String id);
}

getter 和 setter:

public class Profile {
    @SerializedName("PID")
    @Expose
    private String pid;

    @SerializedName("First_Name")
    @Expose
    private String fname;

    @SerializedName("Last_Name")
    @Expose
    private String lname;

    @SerializedName("Team_Lead")
    @Expose
    private String teamlead;

    public Profile(String pid, String fname, String lname, String teamlead) {
        this.pid = pid;
        this.fname = fname;
        this.lname = lname;
        this.teamlead = teamlead;
    }


    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getTeamlead() {
        return teamlead;
    }

    public void setTeamlead(String teamlead) {
        this.teamlead = teamlead;
    }
}

在 TextView 中显示数据:

  private void getProfile(final String id){
    ApiService apiService = ApiClient.getClient().create(ApiService.class);

    Call<Profile> call = apiService.getMyProfile(id);
    call.enqueue(new Callback<Profile>() {
        @Override
        public void onResponse(Call<Profile> call, Response<Profile> response) {

            progressDialog.dismiss();
            Profile p = response.body();

            pid.setText(p.getPid());
            fname.setText(p.getFname());
            lname.setText(p.getLname());
            teamlead.setText(p.getTeamlead());
        }

        @Override
        public void onFailure(Call<Profile> call, Throwable t) {
            //progressDialog.dismiss();
            Toast.makeText(ProfilePage.this, "Failed to load." + t, Toast.LENGTH_LONG).show();
    }
    });
}

我尝试向 TextViews 显示的 JSON 字段:

我收到以下错误消息:

【问题讨论】:

  • 将 json 发布为代码而不是图像
  • 您试图将 json 响应解析为对象,其中 json 响应包含一个数组(以 [] 开头和结尾)。我认为你有两个选择。 1. 将您的 json 响应更改为对象(只需删除 [] )。 2. 改变你的解析器来解析 json 数组而不是对象。

标签: php android json android-studio retrofit2


【解决方案1】:

使用 List 因为它的数组和解析对象的数组:

public interface ApiService {
@FormUrlEncoded
@GET("/lara/getprofile.php?id=")
Call<List<Profile>> getMyProfile(@Query("id") String id);
}

改为:

public interface ApiService {
@FormUrlEncoded
@GET("/lara/getprofile.php?id=")
Call<Profile> getMyProfile(@Query("id") String id);
}

【讨论】:

  • 感谢@R2R!我快到了。不确定如何显示到 TextViews。 @Override public void onResponse(Call> call, Response> response) { Profile p = response.body(); ---在这一行得到错误,不兼容的类型。找到 java.util.List psid.setText(p.getPsid()); fname.setText(p.getFname()); lname.setText(p.getLname()); teamlead.setText(p.getTeamlead()); }
  • 您已经像数组一样检索...response.get(0).getPsid();....0 是索引 ArrayList profileList=response.body();如果它有帮助同意答案
  • 对不起,我不确定我在做什么。更新 Apiservice 后,我的回调方法出现错误,因此我尝试通过设置其中一个文本视图的值来使用它,但运行应用程序时没有任何反应。这是我的回调方法的更新:
  • private void getProfile(final String id){ ApiService apiService = ApiClient.getClient().create(ApiService.class); Call> call = apiService.getMyProfile(id); call.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response>response) { psid.setText(id); }跨度>
【解决方案2】:

您做错了,因为您将响应解析为 Profile 的对象,但实际上,数组中的响应意味着您需要将响应解析为数组,如下所示替换您的 API 服务,并将调用更改为如下所示

public interface ApiService {
    @FormUrlEncoded
    @GET("/lara/getprofile.php?id=")
    Call<List<Profile>> getMyProfile(@Query("id") String id);
}



private void getProfile(final String id){
  ApiService apiService = ApiClient.getClient().create(ApiService.class);
    Call<List<Profile>> call = apiService.getMyProfile(id);
    call.enqueue(new Callback<List<Profile>>() {
        @Override
        public void onResponse(Call<List<Profile>> call, Response<List<Profile>> response) {

            progressDialog.dismiss();
            List<Profile> p = response.body();
            if(p!=null && p.size()>0){
            pid.setText(p.get(0).getPid());
            fname.setText(p.get(0).getFname());
            lname.setText(p.get(0).getLname());
            teamlead.setText(p.get(0).getTeamlead());}
        }

        @Override
        public void onFailure(Call<List<Profile>> call, Throwable t) {
            //progressDialog.dismiss();
            Toast.makeText(ProfilePage.this, "Failed to load." + t, Toast.LENGTH_LONG).show();
    }
    });
}

【讨论】:

  • 谢谢!到目前为止,这是我对回调方法所做的,但我无法弄清楚出了什么问题。 private void getProfile(final String id){ ApiService apiService = ApiClient.getClient().create(ApiService.class); Call> call = apiService.getMyProfile(id); call.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response>response) { }
  • 行 Profile p = response.body();是现在唯一有错误的人。消息:不兼容的类型,找到:java.util.list
  • 我通过使用以下代码得到了@Dhaval: List person = response.body(); progressDialog.dismiss(); // 可以遍历列表并从 POJO 中获取 Getter for(Profile p: person){ fname.setText(p.getFname().toString()); lname.setText(p.getLname().toString()); }
  • 感谢您的帮助!
猜你喜欢
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
  • 2020-01-06
相关资源
最近更新 更多