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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效