感谢http://www.jsonschema2pojo.org/
,我已经在自己的项目中创建了班级模型下面的json是我必须从中获取数据的jsonArray。
{
"response": true,
"timestamp": "23-11-2018 01:21:13",
"status": 200,
"data_response": {
"responseCapaDigital": {
"responseDescription": "Operación exitosa",
"responseID": 1
},
"encabezadoResp": {
"tscNum": "0",
"tscAlfa": "Operación exitosa",
"fechaRsp": "2018-11-23T01:21:12.878"
},
"codeResponseFI": "0000",
"descriptionResponseFI": "Bancos obtenidos correctamente",
"totalBanks": "1",
"bankList": [
{
"bankCode": "421",
"bankName": "GEM-HSBC2",
"bankShortName": "421"
}
]
},
"description": "GET BANK CATALOG"
}
这应该工作的方式是当我发送一个BankCode例如“ 443”时,该服务将返回一个类似于上面的Json数组。
我的connectionHanlder包含:
@Headers({"Content-Type: application/json",
"Authorization: Basic eE9IMC5JdlJaRFZaVTNkV05YazUwZHltVk9MTz"})
@POST("catalog/getCatalogoBancos")
Call<ResponseServiceModel> getCatalogoBancos(@Body String body);
我的RetrofitClient:
public static final String BASE_URL = "http://my_ip:8484/v1/";
private static Retrofit retrofit = null;
final static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(120, TimeUnit.SECONDS)
.connectTimeout(120, TimeUnit.SECONDS)
.build();
public static Retrofit getApi() {
if (retrofit == null) {
GsonBuilder builder = new GsonBuilder();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(builder.create()))
.client(getUnsafeOkHttpClient())
.build();
}
return retrofit;
}
public static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[0];
}
}};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts,
new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext
.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient = okHttpClient.newBuilder()
.sslSocketFactory(sslSocketFactory)
.readTimeout(120, TimeUnit.SECONDS)
.connectTimeout(120, TimeUnit.SECONDS)
.hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
ResponseServiceModel:
@SerializedName("response")
@Expose
private Boolean response;
@SerializedName("timestamp")
@Expose
private String timestamp;
@SerializedName("status")
@Expose
private Integer status;
@SerializedName("data_response")
@Expose
private DataResponse dataResponse;
@SerializedName("description")
@Expose
private String description;
public Boolean getResponse() {
return response;
}
public void setResponse(Boolean response) {
this.response = response;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public DataResponse getDataResponse() {
return dataResponse;
}
public void setDataResponse(DataResponse dataResponse) {
this.dataResponse = dataResponse;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
DataResponse:
@SerializedName("responseCapaDigital")
@Expose
private ResponseCapaDigital responseCapaDigital;
@SerializedName("encabezadoResp")
@Expose
private EncabezadoResp encabezadoResp;
@SerializedName("codeResponseFI")
@Expose
private String codeResponseFI;
@SerializedName("descriptionResponseFI")
@Expose
private String descriptionResponseFI;
@SerializedName("totalBanks")
@Expose
private String totalBanks;
@SerializedName("bankList")
@Expose
private List<BankList> bankList;
public ResponseCapaDigital getResponseCapaDigital() {
return responseCapaDigital;
}
public void setResponseCapaDigital(ResponseCapaDigital responseCapaDigital) {
this.responseCapaDigital = responseCapaDigital;
}
public EncabezadoResp getEncabezadoResp() {
return encabezadoResp;
}
public void setEncabezadoResp(EncabezadoResp encabezadoResp) {
this.encabezadoResp = encabezadoResp;
}
public String getCodeResponseFI() {
return codeResponseFI;
}
public void setCodeResponseFI(String codeResponseFI) {
this.codeResponseFI = codeResponseFI;
}
public String getDescriptionResponseFI() {
return descriptionResponseFI;
}
public void setDescriptionResponseFI(String descriptionResponseFI) {
this.descriptionResponseFI = descriptionResponseFI;
}
public String getTotalBanks() {
return totalBanks;
}
public void setTotalBanks(String totalBanks) {
this.totalBanks = totalBanks;
}
public List<BankList> getBankList() {
return bankList;
}
public void setBankList(List<BankList> bankList) {
this.bankList = bankList;
}
EncabezadoResp:
@SerializedName("tscNum")
@Expose
private String tscNum;
@SerializedName("tscAlfa")
@Expose
private String tscAlfa;
@SerializedName("fechaRsp")
@Expose
private String fechaRsp;
protected EncabezadoResp(Parcel in) {
tscNum = in.readString();
tscAlfa = in.readString();
fechaRsp = in.readString();
}
public String getTscNum() {
return tscNum;
}
public void setTscNum(String tscNum) {
this.tscNum = tscNum;
}
public String getTscAlfa() {
return tscAlfa;
}
public void setTscAlfa(String tscAlfa) {
this.tscAlfa = tscAlfa;
}
public String getFechaRsp() {
return fechaRsp;
}
public void setFechaRsp(String fechaRsp) {
this.fechaRsp = fechaRsp;
}
ResponseCapaDigital:
@SerializedName("responseDescription")
@Expose
private String responseDescription;
@SerializedName("responseID")
@Expose
private Integer responseID;
public String getResponseDescription() {
return responseDescription;
}
public void setResponseDescription(String responseDescription) {
this.responseDescription = responseDescription;
}
public Integer getResponseID() {
return responseID;
}
public void setResponseID(Integer responseID) {
this.responseID = responseID;
}
BankList:
@SerializedName("bankCode")
@Expose
private String bankCode;
@SerializedName("bankName")
@Expose
private String bankName;
@SerializedName("bankShortName")
@Expose
private String bankShortName;
public String getBankCode() {
return bankCode;
}
public void setBankCode(String bankCode) {
this.bankCode = bankCode;
}
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public String getBankShortName() {
return bankShortName;
}
public void setBankShortName(String bankShortName) {
this.bankShortName = bankShortName;
}
最后是我在RequestMethods类中拥有的方法,在其中进行调用:
public void getBankNickName(final String bankId) {
loadingDialog.show();
JSONObject jsonCatalog = new JSONObject();
try {
jsonCatalog.put("bankId", bankId);
} catch (JSONException e) {
e.printStackTrace();
}
responseService = conectionHandler.getCatalogoBancos(jsonCatalog.toString());
responseService.enqueue(new Callback<ResponseServiceModel>() {
@Override
public void onResponse(Call<ResponseServiceModel> call, Response<ResponseServiceModel> response) {
Log.e("Catalog Code ", String.valueOf(response.code()));
if (response.isSuccessful()) {
ResponseServiceModel responseServiceModel = response.body();
if (responseServiceModel.getResponse().equals(true)) {
Log.e("BankName",responseServiceModel.getDataResponse().getBankList().getBankName());
loadingDialog.dismiss();
} else {
loadingDialog.dismiss();
responseError(response.body().getMessage());
}
} else {
loadingDialog.dismiss();
serverError();
}
}
@Override
public void onFailure(Call<ResponseServiceModel> call, Throwable t) {
}
});
}
当我在RequestMethods类中调用getBankNickName时,它将打印我的Log "Catalog code"
但不打印Log"BankName"
我知道这是很多代码,但是我无法弄清楚!!!请帮助
答案 0 :(得分:0)
您需要从mavenCentral()
中添加gson-converter,以消耗JSON
:
dependencies {
api "com.squareup.retrofit2:retrofit:2.4.0"
api "com.squareup.retrofit2:converter-gson:2.4.0"
}
然后使用jsonschema2pojo来创建所需的对象映射。