0

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
Roga Lu
  • 162
  • 1
  • 3
  • 14
  • What is the error message? Please post the exception stack trace and more details about the service response format. – Mushtu Jul 17 '18 at 19:05
  • No error, I just cannot acces to the onRespond method. Always access to the onFailure. But If I try to access to "api/keyIndicator/ . The conection is successful. I dont know what Im doing wrong. – Roga Lu Jul 17 '18 at 19:19
  • In the onFailure method, you are provided a throwable object please print its stack trace and post to your question. – Mushtu Jul 17 '18 at 19:36
  • I really dont know what are you asking for. I already paste the stack trace from the analyze menu. – Roga Lu Jul 17 '18 at 19:41
  • Add t.printStackTrace() in onFailure() method. it will print the trace of error in the logcat. – Mushtu Jul 17 '18 at 19:59
  • I think I know why the problem is. But I will print what you asked me. Just to know if Im right. – Roga Lu Jul 17 '18 at 20:03
  • Thank you for you help Mushtu. If it true you didnt post the solution, your comment about print the stack trace help me to find the solution: https://stackoverflow.com/questions/36177629/retrofit2-android-expected-begin-array-but-was-begin-object-at-line-1-column-2 Thank you. – Roga Lu Jul 17 '18 at 20:19
  • It's good you found the problem. If my comment was helpful please vote up the comment, thank you ;) – Mushtu Jul 17 '18 at 20:41
  • Sorry, cant not vote. – Roga Lu Jul 17 '18 at 20:44

0 Answers0