1

When make GET request on this url I get JSON:

http://52.74.115.250:8080/api/orders?startDay=1&startYear=2017&startMonth=1&endYear=2018&endMonth=2&endDay=9&isAll=true"

When I tried this with the retrofit library in android I get this error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

My code is below.

RetrofitClient.java

public class RetrofitClient {
   public static Retrofit retrofit;
   public static Gson gson;

   public static Retrofit getClient(String baseUrl)
   {

       if(retrofit==null) {
          gson = new GsonBuilder() .setLenient().create();

           retrofit = new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create(gson)).build();
       }
       return retrofit;
   }

}

ApiInterface.java

public interface ApiInterface {
    @FormUrlEncoded
    @POST("local?")
    Call<AuthResponse> loginm(@Field("email") String username, @Field("password") String password);
    @GET("/orders?startDay=1&&startYear=2017&&startMonth=1&&endDay=1&&endYear=2018&&endMonth=1")
    Call<OrderList> getOrder(@Header("Authorization") String authorization, @Header("language")String language);

}

ApiUtils.java

public class ApiUtils {
    public static final String BASE_URL = "http://52.74.115.250:8080/";
    static public String loginUrlStr = BASE_URL +"auth/";

   static public String dealStr=BASE_URL+"api/";
    public static ApiInterface getSOService(String url)
    {
        return RetrofitClient.getClient(url).create(ApiInterface.class);

    }
}

This is how I am making request in MainActivity.java:

myInterface.getOrder(token,"english").enqueue(new Callback<OrderList>() {

                        @Override
                        public void onResponse(Call<OrderList> call, Response<OrderList> response) {
                            if(response.isSuccessful())
                            {
                                Log.d("resultorder","RESULT IS "+response.body().getOrders().size());
                            }

                        }

                        @Override
                        public void onFailure(Call<OrderList> call, Throwable t) {

                        }
                    });
Community
  • 1
  • 1
user3792429
  • 203
  • 1
  • 16
  • post this url in browser and post your JSON please – Adil Feb 09 '18 at 03:52
  • if you need to go step by step follow this -https://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request – Adil Feb 09 '18 at 03:53
  • use updated retrofit 2.0.2 – Ankesh Roy Feb 09 '18 at 05:28
  • @Adilhusen:When I pasted the url in browser it gave "unauthorized"..But I am adding authentication header in my retrofit get request,where the authorization value is the token returned after we log in succesfully. – user3792429 Feb 09 '18 at 15:23
  • Okay then first you need to check in postman, and post your json response in your question – Adil Feb 09 '18 at 17:08

1 Answers1

0

Im guessing your response json is not fully formatted json. please check if you really get the output correctly

example, extra configure

public Gson provideGson() {
        GsonBuilder builder = new GsonBuilder();
        builder.setLenient();
        builder.registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> {
            if (json.getAsJsonPrimitive().isNumber()) {
                return new Date(json.getAsJsonPrimitive().getAsLong() * 1000);
            } else {
                return null;
            }
        });
        return builder.create();
    }

use as

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

add this dependencies to log every http request/response you made. With this, you will know the response you get is correct or not

implement "com.squareup.okhttp3:logging-interceptor:${retrofitLoggingVersion}"
ZeroOne
  • 8,996
  • 4
  • 27
  • 45