dtcd27183 2018-04-10 05:09
浏览 153
已采纳

无法加载load.com.google.gson.JsonSyntaxException | 预期BEGIN_OBJECT但在第1行第2列路径$是BEGIN_ARRAY

I'm trying to display data into TextViews. I'm new to Retrofit and would appreciate any help and tip(s). The following are what I have so far.

API Client:

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 Service:

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

Getter and 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;
    }
}

Displaying data into TextViews:

  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();
    }
    });
}

JSON fields that I'm trying to display to TextViews:

JSON

I'm getting the following error message:

image

  • 写回答

2条回答 默认 最新

  • dsgw8802 2018-04-10 05:35
    关注

    You are doing wrong because you are parsing your response As Object for Profile but actually, a response in the array means you need to parse response as an array, replace your API service as bellow also change calling as bellow

    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();
        }
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置