代码之家  ›  专栏  ›  技术社区  ›  Kent

未能加载。通用域名格式。谷歌。格森。JsonSyntaxException |应为BEGIN\u对象,但在第1行第2列路径处为BEGIN\u数组$

  •  0
  • Kent  · 技术社区  · 6 年前

    我正在尝试将数据显示到 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);
    }
    

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

    将数据显示到文本视图中:

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

    我试图向TextView显示的JSON字段:

    JSON

    我收到以下错误消息:

    image

    2 回复  |  直到 6 年前
        1
  •  2
  •   Ramesh sambu    6 年前

    使用List是因为它的数组和解析objecct的数组:

    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);
    }
    
        2
  •  1
  •   Dhaval Solanki Sandunika Wijerathne    6 年前

    您做错了,因为您正在将响应解析为的对象 Profile 但实际上,数组中的响应意味着您需要将响应解析为数组,将API服务替换为bellow,也将调用更改为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();
        }
        });
    }