1

I've already read this question Google Map Direction Api using Retrofit I want to get the time and the distance so I've created these model classes

Distance.java

public class Distance{
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @SerializedName("text")
    private String text;

    @SerializedName("value")
    private int value;
}

Duration.java

public class Duration{
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @SerializedName("text")
    private String text;

    @SerializedName("value")
    private int value;
}

Legs.java

public class Legs{
    private Distance distance;
    private Duration duration;

    public Distance getDistance() {
        return distance;
    }

    public void setDistance(Distance distance) {
        this.distance = distance;
    }

    public Duration getDuration() {
        return duration;
    }

    public void setDuration(Duration duration) {
        this.duration = duration;
    }
}

Route.java

public class Route{
    @SerializedName("legs")
    private List<Legs> legses;

    public List<Legs> getLegses() {
        return legses;
    }

    public void setLegses(List<Legs> legses) {
        this.legses = legses;
    }
}

The interface:

public interface MyApiRequestInterface {

    @GET("maps/api/directions/json")
    public void getJson(@Query("origin") String origin, @Query("destination") String destination, Callback<DirectionResults> callback);
}

and in the fragment:

String base_url = "http://maps.googleapis.com/";

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(base_url)
                .build();

        MyApiRequestInterface reqinterface = retrofit.create(MyApiRequestInterface.class);
        String origin = "41.2198254,16.3076254";
        String dest = "41.2199,16.3062";

        reqinterface.getJson(origin,dest, new Callback<DirectionResults>() {

            @Override
            public void onResponse(Call<DirectionResults> call, Response<DirectionResults> response) {
           Log.d("CallBack", " response is " + response);
            }

            @Override
            public void onFailure(Call<DirectionResults> call, Throwable t) {
             Log.d("CallBack", " Throwable is " +t);
            }
        });

Running the app I've the following error:

java.lang.IllegalArgumentException: Service methods cannot return void.
                                                                                                   for method MyApiRequestInterface.getJson
                                                                                                   at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
                                                                                                   at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711)
                                                                                                   at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:228)
                                                                                                   at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:160)
                                                                                                   at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
                                                                                                   at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
                                                                                                   at java.lang.reflect.Proxy.invoke(Proxy.java:397)
                                                                                                   at $Proxy1.getJson(Unknown Source)
                                                                                                   at it.uniba.di.ivu.sms16.gruppo26.it.andriasmart.Fragment.BusFragment.using_retrofit(BusFragment.java:369)
                                                                                                   at it.uniba.di.ivu.sms16.gruppo26.it.andriasmart.Fragment.BusFragment$7$1$2.onMarkerClick(BusFragment.java:323)
                                                                                                   at com.google.android.gms.maps.GoogleMap$15.zzd(Unknown Source)
                                                                                                   at com.google.android.gms.maps.internal.zzp$zza.onTransact(Unknown Source)
                                                                                                   at android.os.Binder.transact(Binder.java:395)
                                                                                                   at yc.a(:com.google.android.gms.DynamiteModulesB:84)
                                                                                                   at maps.ad.T.b(Unknown Source)
                                                                                                   at maps.D.d.a(Unknown Source)
                                                                                                   at maps.V.k.a(Unknown Source)
                                                                                                   at maps.V.u.d(Unknown Source)
                                                                                                   at maps.V.P.onSingleTapConfirmed(Unknown Source)
                                                                                                   at maps.z.e$b.onSingleTapConfirmed(Unknown Source)
                                                                                                   at maps.z.c$a.handleMessage(Unknown Source)
                                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                   at android.os.Looper.loop(Looper.java:145)
                                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6873)
                                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

--EDIT--

I create the interface in this way:

public interface Api {
    @GET("maps/api/directions/json")
    Call<DirectionResults> getJson(@Query("origin") String origin, @Query("destination") String destination);
}

in the fragment:

String base_url = "http://maps.googleapis.com/";

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        String origin = "41.2198254,16.3076254";
        String dest = "41.2199,16.3062";

        Api service = retrofit.create(Api.class);
service.getJson(origin,dest).enqueue(new Callback<DirectionResults>() {
    @Override
    public void onResponse(Call<DirectionResults> call, Response<DirectionResults> response) {
        Log.d("CallBack", " response is " + response);
        Route routeA = response.body().getRoutes().get(0);
        Legs legs =routeA.getLegses().get(0);


    }

    @Override
    public void onFailure(Call<DirectionResults> call, Throwable t) {
        Log.d("CallBack", " Throwable is " +t);
    }
});

Now works! In the example above I used a fixed origin and destination, but if I want to iterate an arraylist, calculate the distance of each items and pass the data to the adapter, what can I do?

Community
  • 1
  • 1
Fidelis
  • 91
  • 1
  • 11

0 Answers0