在进行Retrofit 2.0 GET请求时,如何修复java.lang.IllegalStateException?

时间:2018-02-09 02:42:05

标签: android retrofit2 gson

当在这个网址上发出GET请求时,我得到了JSON:

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

当我在android中使用改装库尝试此操作时,我收到此错误:

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:   预期BEGIN_OBJECT但在第1行第1行路径$ STR的STRING

我的代码如下。

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);

    }
}

这就是我在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) {

                        }
                    });

1 个答案:

答案 0 :(得分:0)

我猜你的回答json没有完全格式化json。请检查你是否真的得到了正确的输出

示例,额外配置

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();
    }

用作

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

添加此依赖项以记录您所做的每个http请求/响应。有了这个,你就会知道你得到的回答是否正确

implement "com.squareup.okhttp3:logging-interceptor:${retrofitLoggingVersion}"