无法使用改造库显示数据
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 //Example.java 由 Gson 转换器生成:
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'
我是改造新手,我的目标是使用改造库显示来自 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 //Example.java 由 Gson 转换器生成:
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'