我有这种回复。如何处理Retrofit2?
{
"content": {
"customer": null,
"vehicle": null,
"inspectionLibrary": [
{
"id": 761,
"desc": "16 point - GM57",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 758,
"desc": "20 point courtsey",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 762,
"desc": "33 point courtsey",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 766,
"desc": "42 point courtsey",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 747,
"desc": "A/C system",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 869,
"desc": "Alternator",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 749,
"desc": "Battery",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 623,
"desc": "Body, bumper to bumper",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 486,
"desc": "Brakes",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 744,
"desc": "Catalytic converter",
"system": "Catalytic Converter",
"systemId": 474,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 751,
"desc": "Charging system",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 489,
"desc": "Cooling and A/C system",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 591,
"desc": "Driveline, drivetrain",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 618,
"desc": "Exhaust system",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 617,
"desc": "Fluid leaks",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 622,
"desc": "Fluids and filters",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 757,
"desc": "General pre-purchase inspection",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 593,
"desc": "Hybrid vehicle",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 621,
"desc": "Instruments",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 592,
"desc": "Lights",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 867,
"desc": "Nissan Pre-owned",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 863,
"desc": "Oil change",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 745,
"desc": "Oxygen sensor",
"system": "Oxygen Sensor",
"systemId": 427,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 866,
"desc": "Pre-alignment",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 807,
"desc": "Preventive maintenance - 502 force support squadron NAF vehicle fleet",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 764,
"desc": "Quick lube",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 754,
"desc": "Road test",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 619,
"desc": "Starter and charging system",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 750,
"desc": "Starting system",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 487,
"desc": "Steering and suspension system",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 490,
"desc": "Tires",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 752,
"desc": "Total vehicle and all system",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 868,
"desc": "Transmission",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 748,
"desc": "Transmission Inspection",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 746,
"desc": "Tune-up",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 753,
"desc": "Under the hood",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 755,
"desc": "Under vehicle",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 804,
"desc": "Vehicle safety form 502 FSS",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 594,
"desc": "Vehicle stability",
"system": "Road Test",
"systemId": 475,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 810,
"desc": "Vehicle walk around",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 485,
"desc": "Visual inspection",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": null,
"systemFirebaseKey": null
},
{
"id": 620,
"desc": "Wipers",
"system": "Best Practices, General Inspection",
"systemId": 424,
"inspectionFlag": true,
"firebaseKey": "-LDGovCRvp7w9bmw12ii",
"systemFirebaseKey": "-LDGovCRvp7w9bmw12ii"
}
],
"inspectionList": [],
"zoneTime": "2018-08-23T07:21:21.000Z"
},
"status": "success"
}
答案 0 :(得分:0)
首先,创建InspectionLibrary对象
class InspectionLibrary
{
int id;
String desc;
String system;
int systemId;
boolean inspectionFlag;
String firebaseKey;
String systemFirebaseKey;
}
然后是内容对象(不要忘记创建InspectionList对象)。
class ResultContent
{
String customer;
String vehicle;
InspectionLibrary[] inspectionLibrary;
InspectionList[] inspectionList;
String zoneTime;
}
然后是结果对象:
class ApiResult
{
ResultContent content;
String status;
}
最后,从Api界面添加此方法(别忘了重新放置POST批注中的文本):
@POST("Path")
Call<ApiResult> getResult();
答案 1 :(得分:0)
我的理解...您正在进行改造,并希望以json解析的形式获得响应。
为此,首先必须使用http://www.jsonschema2pojo.org/内部包来创建模型类:定义应用程序根包名称。班级名称:提供您想保留的班级名称。但是,请确保该给定的类名将成为模型的根类。如您所见,您的json具有分层模型。
目标语言:Java源类型:Json批注类型:Gson检查:使用双数并包含getter和setter。
这是使用将能够创建Model类的配置。
让我们了解您的json:它将如何工作?假设我使用了包名:com测试示例类名:TestDao
之后,我下载了TestDao-source.zip
现在解压缩此内容:您将看到文件夹代码->测试->示例->文件
Content.java InspectionalLibrary.java TestDao.java
所以我希望你对此有所了解。让我解释一下:看一下您的json。
第1步:在json中,您有两件事内容和状态,即json的根键值。因此,这两件事进入TestDao.java
第2步:下一步是什么?参见...第二层次结构是具有顾客,车辆,inspectionLibrary,inspectionList,zoneTime的“内容”。所有这些键值都位于“ content”中,这就是为什么另一个模型类名称为content.java
第3步:第三个层次是“ inspectionLibrary”,其中包含id,desc,system,systemId,inspectionFlag,firebaseKey,systemFirebaseKey。所有这些键值都位于“ Inspectional Library”中,这就是为什么另一个模型类名称为“ inspectionalLibrary.java”的原因。
第4步:四个层次结构是“ inspectionList”,当前没有任何数据表示空值。因此,一旦将有数组格式的数据,然后像InspectionalLibrary一样,您将不得不制作另一个类并编写这些类的getter setter方法。
@SerializedName(“ content”)@Expose
每个模型类的每个变量都具有这些注释。这表示serializedName,它表示一旦在改造中使用了该模型类,则GSON Parser将会以“ SerializedName(“ Key_Name”)”为目标。意思是括号内的内容只不过是json的键而已,它可以帮助GSON解析您的值。
@Expose有助于使用定义的变量公开解析后的值。
我希望你理解这就是我们必须阅读json的方式。
答案 2 :(得分:0)
现在让我们谈谈改造。
RestAPI.java 这基本上是用于make api的调用。
public class RestClient {
private static TestWebService gitApiInterface ;
private static String baseUrl = "https://example.com" ; // your api base url
public static TestWebService getClient() {
if (gitApiInterface == null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
httpClient.connectTimeout(30, TimeUnit.SECONDS);
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
gitApiInterface = client.create(TestWebService.class);
}
return gitApiInterface ;
}
public interface TestWebService
{
@POST("/subtypeindex/{param1}/{param2}/")
Call<TestDao> getSampleApiResponse(@Path(value = "param1") String param1,
@Path(value = "param1") String param2);
}
}
请参见接口TestWebService的内部,我编写了一个方法“ getSampleApiResponse”,该方法返回TestDao(之前我曾解释过这是模型的根类)。在@POST中,您可以修改其余的api url,但在类开始时定义的基址除外。您可以根据需要定义参数。可以使用不同形式的参数分配。供参考,请通过改造官方网站。
这是您可以从活动或片段中调用的方法。现在在这里,您需要确保从接口返回的任何内容都与Call中具有映射的类相同。这就是它将与您的界面链接并帮助您调用定义api的方式。
private void callTestApi()
{
RestClient.TestWebService service = RestClient.getClient();
Call<TestDao> callTestApi = service.getSampleApiResponse("param1", "param2");
callTestApi.enqueue(new Callback<TestDao>()
{
@Override
public void onResponse(Call<TestDao> call, Response<TestDao> response)
{
if(response.raw().code()==200)
{
if(response.body().getStatus().toString().equalsIgnoreCase("1"))
{
mUserMenuAdapter = new UserMenuViewAdapter(MainActivity.this,
response.body().getContent().getInspectionList());
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new
LinearLayoutManager(MainActivity.this));
mRecyclerView.setAdapter(mUserMenuAdapter);
}
else
{
Toast.makeText(getApplicationContext(),"Prompt Error
Msg",Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(),"Prompt Error
Msg",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<TestDao> call, Throwable t)
{
t.printStackTrace();
}
});
}
我希望这可以帮助您了解使用翻新的api调用的基本流程以及如何在使用前读取json。