Android 如何通过改造安卓系统提出请求
这是我的Api响应,我不知道它是Android 如何通过改造安卓系统提出请求,android,json,rest,get,retrofit,Android,Json,Rest,Get,Retrofit,这是我的Api响应,我不知道它是json对象还是json数组 我需要创建一个带有改装的GET请求,但我不知道 { "Customers": [ { "Empresa": "CORSENESA", "Cliente": "1000 ", "Nombre": "ANDREA ARRIOLA LOPEZ
json对象
还是json数组
我需要创建一个带有改装的GET
请求,但我不知道
{
"Customers": [
{
"Empresa": "CORSENESA",
"Cliente": "1000 ",
"Nombre": "ANDREA ARRIOLA LOPEZ ",
"Direccion": "7 AV. 9-64 ZONA 9\r\n\r\n",
"telefonos": "4710-7975 ",
"DiasCredito": 0,
"inactivo": false,
"DiasVisita": "L,M,MI,J,V,S,D"
},
{
"Empresa": "CORSENESA",
"Cliente": "1005 ",
"Nombre": "TANIA LOPEZ HERNANDEZ STA. PAULA",
"Direccion": "PASEO SAN CRISTOBAL ZONA 8 DE MIXCO",
"telefonos": "53071090 ",
"DiasCredito": 0,
"inactivo": false,
"DiasVisita": "L,M,MI,J,V,S,D"
}
]
}
作为一个严肃的开发者对开发者的建议,你需要学习你的基础知识。为了帮助您,上面是JSON数组,我将告诉您步骤 (假设您使用的是android studio) 在你的应用程序中:gradle文件
接口yourinterfacename{
@获取(“基本url之后的url路径”)
调用getData();
}
/*baseurl:比如说:yourdomain.com/user/data那么yourdomain.com/就是你的基本url
&用户/数据将是您的路径*/
将YourResponseModels分类{
私人列表客户;/*确保“客户”与响应中提到的相同,否则您将无法获得任何数据*/
公共静态类数据{
私人字符串Empresa;
…/*在响应中添加所有参数,确保参数名称与api响应中提到的完全匹配*/
//实现getter和setter
}
}
现在,一旦您这样做了,您需要构建一个对象
,
retroObject=new reformation 2.reformation.Builder()
.baseUrl(基本URL)
.client(httpClient.build())
.addConverterFactory(
GsonConverterFactory.create(新建Gson())
.build()
/*确保在另一个类中使用它,并在该类中实现静态方法,以便可以使用返回类型翻新获得retro对象的实例
类似->公共静态改装getRefughtInstance(){}
*/
,
现在让我们进行一个API调用
YourInterfaceName service = RetroClassIntancename.YourRetroObjectMethodName().create(YourInterfaceName.class)
Call<yourinterfacename> call = service.getData() // remember the method name you created in your interface
call.enqueue(new Callback<YourResponseClassName>() {
@Override
public void onResponse(Response<YourResponseClassName> response, Retrofit retrofit) {
// your code if it is successfull
}
@Override
public void onFailure(Throwable t) {
// if it fails
}
});
YourInterfaceName服务=RetroClassIntancename.YourRetroObjectMethodName().create(YourInterfaceName.class)
Call Call=service.getData()//记住在接口中创建的方法名
call.enqueue(新回调(){
@凌驾
公共响应(响应、改装){
//如果成功,请输入您的代码
}
@凌驾
失效时的公共无效(可丢弃的t){
//如果失败了
}
});
以上是粗略的实现。有很多很好的教程可以参考
希望这有帮助。快乐编码:)你能说得更具体一点吗?它是一个Json对象,里面有Json数组。
interface yourinterfacename {
@GET("your url path after base url")
Call<YourResponseModelCalss> getData();
}
/* baseurl: Say Eg: yourdomain.com/user/data then yourdomain.com/ is ur base url
& user/data will be your path */
class YourResponseModelCalss {
private List<RespData> Customers; /* make sure 'Customers' is same as mentioned in response else you will not get any data.*/
public static class RespData {
private String Empresa;
... /* add all your paramters as in the response, make sure parameters name should match exact as mentioned in api response */
// implement getters and setters
}
}
YourInterfaceName service = RetroClassIntancename.YourRetroObjectMethodName().create(YourInterfaceName.class)
Call<yourinterfacename> call = service.getData() // remember the method name you created in your interface
call.enqueue(new Callback<YourResponseClassName>() {
@Override
public void onResponse(Response<YourResponseClassName> response, Retrofit retrofit) {
// your code if it is successfull
}
@Override
public void onFailure(Throwable t) {
// if it fails
}
});