I'm new working with retrofit in android, I have read the following links:
https://github.com/codepath/android_guides/wiki/Consuming-APIs-with-Retrofit https://inthecheesefactory.com/blog/retrofit-2.0/en Is it possible to manipulate retrofit url without method parameter?
But I can not make my code working. Any help is very good recived.
My service ProductInfo:
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface ProductInfo {
@GET("api/keyIndicator/{id}/")
Call<List<ProductManager>> getFindProduct (@Path(value = "id") String
productId);
}
My method inside my java class:
private void FillInfoProduct(String cveProduct) {
String baseUrl = "http://SOMEURL/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
ProductInfo productInfo = retrofit.create(ProductInfo.class);
Call<List<ProductManager>> productId =
productInfo.getFindProduct(cveProduct);
productId.enqueue(new Callback<List<ProductManager>>() {
@Override
public void onResponse(Call<List<ProductManager>> call,
Response<List<ProductManager>> response) {
System.out.println("FINDED");
if (response.isSuccessful()) {
productFinded=response.body();
}
}
@Override
public void onFailure(Call<List<ProductManager>> call, Throwable t) {
System.out.println("NOT FINDED");
}
});
}
The JSON I recive:
[{"Id":1,"Name":"Product1","Price":9.99},
{"Id":2,"Name":"Product2","Price":19.99},
{"Id":3,"Name":"Product3","Price":29.99},
{"Id":4,"Name":"Product4","Price":39.99},
{"Id":5,"Name":"Product5","Price":49.99},
...]
I wanna access to the id URL, something like this:
http://SOMEURL/api/keyIndicator/1
http://SOMEURL/api/keyIndicator/2
.
.
.
http://SOMEURL/api/keyIndicator/1000
And read:
{"Id":1,"Name":"Product1","Price":9.99}
{"Id":2,"Name":"Product2","Price":19.99}
etc
The logcat always return: NOT FINDED THERE IS NO ERROR. I always access to the onFailure method.
Stack trace
I/Choreographer: Skipped 33 frames! The application may be doing too much work on its main thread.
D/EGL_emulation: eglMakeCurrent: 0xa76061e0: ver 2 0 (tinfo 0xa7609120)
I/AssistStructure: Flattened final assist data: 7732 bytes, containing 1 windows, 33 views
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/System.out: NOT FINDED
Stack trace from:
public void onFailure(Call<List<ProductManager>> call, Throwable t)
07-17 19:56:26.354 9860-
9860/com.example.sislarodriguez.aplicacionessimilaresv1 I/System.out:
CONNECTION ERROR: call:
retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@d951a8aThrowable:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at
line 1 column 2 path $
ONNECTION ERROR: call:
retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@161ccf5Throwable:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at
line 1 column 2 path $
07-17 20:04:35.049 10189-
10189/com.example.sislarodriguez.aplicacionessimilaresv1 W/System.err:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at
line 1 column 2 path $
07-17 20:04:35.050 10189-
10189/com.example.sislarodriguez.aplicacionessimilaresv1 W/System.err: at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
07-17 20:04:35.051 10189-10189/com.example.sislarodriguez.aplicacionessimilaresv1 W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:122)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:217)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:116)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
07-17 20:04:35.052 10189-10189/com.example.sislarodriguez.aplicacionessimilaresv1 W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
07-17 20:04:53.116 10189-10194/com.example.sislarodriguez.aplicacionessimilaresv1 I/zygote: Do full code cache collection, code=499KB, data=305KB
07-17 20:04:53.117 10189-10194/com.example.sislarodriguez.aplicacionessimilaresv1 I/zygote: After code cache collection, code=483KB, data=262KB