gpt4 book ai didi

java - Retrofit 2 返回 HTML 而不是 JSON

转载 作者:行者123 更新时间:2023-12-02 02:34:23 29 4
gpt4 key购买 nike

我正在使用 Retrofit 2 向 ASP.NET API 发出 POST 请求,我得到的是 HTML 响应而不是 JSON。如果我更改目标 URL 并调用不同的 API 并获取 JSON 响应

这是我的API接口(interface)

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

休息经理

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

执行我的登录()

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

最佳答案

请在改造onResponse中添加以下语句

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

或者如果您只想接受响应 200

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

关于java - Retrofit 2 返回 HTML 而不是 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46614034/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com