简体   繁体   中英

Retrofit 2 returns HTML instead of JSON

I am making a POST Request to an ASP.NET API using Retrofit 2, I am getting an HTML in response instead of JSON. If I change the target URL and call a different API and get JSON response

Here is my API interface

@POST("PosLogin")
Call<CinekinRequest> login();   

Rest Manager

public static final String BASE_URL = "******************";

private API homeApi;

public API getAPi() {
if (homeApi == null) {

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

logging.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

httpClient.addInterceptor(logging);         
Gson gson = new GsonBuilder()
.setLenient()
.create();


Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build();

homeApi = retrofit.create(API.class);
                }
return homeApi;
            }

Executing my login()

public void login(final Context context, CinekinRequest login){

Log.e("login", "starting");

Call call = manager.getAPi().login( );
call.enqueue(new Callback<CinekinRequest>() {

@Override
public void onResponse(Call<CinekinRequest> call, Response<CinekinRequest> response) {

Toast.makeText(context, "Success " + response.message(), Toast.LENGTH_SHORT).show();
Log.e("res", "success");
}

public void onFailure(Call<CinekinRequest> call, Throwable t) {
        Toast.makeText(context, "error: " + t.getMessage(), Toast.LENGTH_LONG).show();
        Log.e("error", t.getMessage());
        }
        });
    }

Please add statement below in retrofit onResponse

if (response.isSuccessful()) {
       // Do something
    } else {
       // Do something
    }

or if you want only accept response 200

 if (response.code() == 200) {
       // Do something
    } else {
       // do something
    }

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