[英]Retrofit Android
Sorry for my bad English 对不起,我的英语不好
I have a web service that delivers in the following format in json, I want to consume it from my android app using retrofit. 我有一个以json格式提供以下内容的网络服务,我想使用改造功能从我的android应用中使用它。
{"datos":
[{"codigo_centro":"Plantacion","descripcion_articulo":"Banano", "categoria_centro":"Admin"},
{"codigo_centro":"Finca","descripcion_articulo":"Finca ","categoria_centro":"Admin"},
{"codigo_centro":"Pante","descripcion_articulo":"Pante","categoria_centro":"Admin"}
],"error":false}
This is my apiservice 这是我的apiservice
@GET("obtener_centro.php")
Call<CentroResponse> getDatos();
Response 响应
@SerializedName("datos")
private ArrayList<Centro> datos;
private boolean Error;
Callback 打回来
class CentroCallBack implements Callback<CentroResponse> {
@Override
public void onResponse(Call<CentroResponse> call, Response<CentroResponse> response) {
if (response.isSuccessful()) {
CentroResponse centroResponse = response.body();
if(!centroResponse.isError()) {
if(centroResponse.getDatos()!=null){
// poblarSpinnerCentro(centroResponse.getDatos());
}
}
}else{
Toast.makeText(reporteActivity.this, "Ha Ocurrido un Error en el Formato de Respuesta", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<CentroResponse> call, Throwable t) {
Toast.makeText(reporteActivity.this, "Fallo la conexion a Internet: "+call.request().url()+" "+t.getMessage(), Toast.LENGTH_SHORT).show();
}
}
public void obtenerCentros() {
try{
Call<CentroResponse> call = OPCApiAdapter.getApiService().getDatos();
call.enqueue(new CentroCallBack());
}catch(Exception e){
Log.e("Error",e.getMessage());
}
}
when I try to go through my arrayList all the data is null 当我尝试遍历arrayList时,所有数据为null
Any idea what I'm doing wrong? 知道我在做什么错吗?
this is the answer I receive in logcat 这是我在logcat中收到的答案
11-02 18:23:19.339 25501-25849/com.fgtit D/OkHttp: Transfer-Encoding: chunked
11-02 18:23:19.339 25501-25849/com.fgtit D/OkHttp: Content-Type: application/json
11-02 18:23:19.352 25501-25849/com.fgtit D/OkHttp: {"datos":
[{"codigo_centro":"Plantacion","descripcion_articulo":"Banano", "categoria_centro":"Admin"},
{"codigo_centro":"Finca","descripcion_articulo":"Finca ","categoria_centro":"Admin"},
{"codigo_centro":"Pante","descripcion_articulo":"Pante","categoria_centro":"Admin"}
],"error":false} 11-02 18:23:19.353 25501-25849/com.fgtit D/OkHttp: <-- END HTTP (31547-byte body)
11-02 18:23:19.518 25501-25501/com.fgtit V/ActivityThread: Finishing stop of ActivityRecord{3430af23 token=android.os.BinderProxy@26296c20 {com.fgtit/report.activity.activityMenu}}: show=false win=com.android.internal.policy.impl.PhoneWindow@1f2d0d4c
First of all, you must check if your app reaches your API. 首先,您必须检查您的应用程序是否符合API。 To do so, you can drop a breakpoint on the
if (response.isSuccessful()) {
line, and another one on Toast.makeText
line. 为此,可以在
if (response.isSuccessful()) {
行上放置一个断点,并在Toast.makeText
行上Toast.makeText
另一个Toast.makeText
。 Then debug your project, so you'll see your API response. 然后调试您的项目,以便您看到API响应。
After that, check if the response is successful, if so, check your response.body
. 之后,检查响应是否成功,如果成功,则检查您的
response.body
。 if all looks good, please, print your response body and place here. 如果一切看起来不错,请在此处打印您的回复正文和位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.