dtcd27183
dtcd27183
2018-04-10 05:09
浏览 120

无法加载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
    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();
        }
        });
    }
    
    点赞 评论
  • duanbing6955
    duanbing6955 2018-04-10 05:20

    Use List Because its array and parse the array of objecct:

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

    Instead:

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

相关推荐