无法使用改型库显示数据

时间:2018-08-31 09:40:04

标签: java android gson retrofit pojo

我是新手,我的目标是使用改型库显示URL中的数据。

我的Json数据是:

`{
  "RestResponse": {
    "messages": [
      "Total [249] records found."
    ],
    "result": [
      {
        "name": "Afghanistan",
        "alpha2_code": "AF",
        "alpha3_code": "AFG"
      },
      {
        "name": "��land Islands",
        "alpha2_code": "AX",
        "alpha3_code": "ALA"
      },
      {
        "name": "Albania",
        "alpha2_code": "AL",
        "alpha3_code": "ALB"
      },
      {
        "name": "Algeria",
        "alpha2_code": "DZ",
        "alpha3_code": "DZA"
      },`

我想在LogCat中显示国家名称,这是gson转换的Pojo类

public class RestResponse {

    @SerializedName("messages")
    @Expose
    private List<String> messages = null;
    @SerializedName("result")
    @Expose
    private List<Result> result = null;

    public List<String> getMessages() {
        return messages;
    }

    public void setMessages(List<String> messages) {
        this.messages = messages;
    }

    public List<Result> getResult() {
        return result;
    }

    public void setResult(List<Result> result) {
        this.result = result;
    }
}

第二个Result.java:

public class Result {

    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("alpha2_code")
    @Expose
    private String alpha2Code;
    @SerializedName("alpha3_code")
    @Expose
    private String alpha3Code;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAlpha2Code() {
        return alpha2Code;
    }

    public void setAlpha2Code(String alpha2Code) {
        this.alpha2Code = alpha2Code;
    }

    public String getAlpha3Code() {
        return alpha3Code;
    }

    public void setAlpha3Code(String alpha3Code) {
        this.alpha3Code = alpha3Code;
    }

}

,最后是Movies.java //由Gson Converter生成的Example.java:

public class Movies {

    @SerializedName("RestResponse")
    @Expose
    private RestResponse restResponse;

    public RestResponse getRestResponse() {
        return restResponse;
    }

    public void setRestResponse(RestResponse restResponse) {
        this.restResponse = restResponse;
    }
}

从上面的类中,我试图检索数据。我的RetrofitInstance是:

    public class RetrofitInstance {

     private static Retrofit retrofit = null;
        private static String BASE_URL = "http://services.groupkt.com/";
        public static ApiEndpoints getCombine() {
            if (retrofit == null) {

                retrofit = new Retrofit
                        .Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
           return retrofit.create(ApiEndpoints.class);
        }
    } 

和端点接口是:

public interface ApiEndpoints {

    @GET("country/get/all")
    Call<Movies> getResults();
}

在MainActivity中使用以下内容:

        ApiEndpoints getCountryDataService= RetrofitInstance.getCombine();
        Call<Movies> call=getCountryDataService.getResults();
        call.enqueue(new Callback<Movies>() {
            @Override
            public void onResponse(Call<Movies> call, Response<Movies> response) {
                Movies info=response.body();
                if(info !=null && info.getRestResponse() != null){
                    results=(ArrayList<Result>) info.getRestResponse().getResult();
                    for(Result r:results){

                        Log.i("testing123","*********************************"+   r.getName());

                    } 
                } 
            }
            @Override
            public void onFailure(Call<Movies> call, Throwable t) {
                Log.i("Error",t.fillInStackTrace()+"s");
                t.fillInStackTrace(); 
            }
        }); 

最后,我无法在日志中打印国家/地区。我收到以下错误     java.net.UnknownServiceException: CLEARTEXT communication to services.groupkt.com not permitted by network security policys 并且对改造概念感到陌生。

请帮助 预先感谢。

1 个答案:

答案 0 :(得分:0)

我通过从

减少gradle版本解决了我的问题

groups.list

/invite @bot