简体   繁体   中英

Android Retrofit:On Response not calling

I am using retrofit for the first time.I want to get the authentication returned from the Api but onResponse is not called. Here is my code:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://abc.def.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiInterface service = retrofit.create(ApiInterface.class);

        String setAPI_KEY = "123456";
        String settoken = "7891011";
        retrofit2.Call<TokenResponse> tokenResponseCall = service.getToken(setAPI_KEY, settoken);

        tokenResponseCall.enqueue(new Callback<TokenResponse>() {
            @Override
            public void onResponse(retrofit2.Call<TokenResponse> call, retrofit2.Response<TokenResponse> response) {
                int a = response.code();
                TokenResponse tokenResponse = new TokenResponse();

            }

            @Override
            public void onFailure(retrofit2.Call<TokenResponse> call, Throwable t) {
                System.out.print("Fail");
            }
        });

In the Interface I am setting it like this:

public interface ApiInterface {
    @GET("/qwe/asd/zxc")
    Call<TokenResponse> getToken(@Query("key") String API_KEY,@Query("acesstoken") String token);

}

Any idea whats wrong in it? The complete URL is like this

https://abc.def.com/qwe/asd/zxc?key=Key&acesstoken=Token

Here is my TokenResponse Class:

public class TokenResponse {
    @SerializedName("authorization_key")
    private String authorization_key;

    public String getAuthorization_key() {
        return authorization_key;
    }
}

The Exception I am getting is : com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

GSON throws that particular error when there's extra characters after the end of the object that aren't whitespace, and it defines whitespace very narrowly.

Use it

Gson gson = new GsonBuilder()
    .setLenient()
    .create();
Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://abc.def.com/")
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM