簡體   English   中英

無法使用改型庫顯示數據

[英]Not able to display the Data using retrofit library

我是新手,我的目標是使用改型庫顯示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並且是改造概念的新內容。

請事先幫助。

我通過減少gradle版本來解決我的問題

classpath 'com.android.tools.build:gradle:3.1.4'

classpath 'com.android.tools.build:gradle:3.1.2'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM