0
我正在使用Retrofit 2向ASP.NET API发送POST请求,我得到的是响应中的HTML而不是JSON。如果我更改目标URL,并调用不同的API,并得到JSON响应Retrofit 2返回HTML而不是JSON
这里是我的API接口
@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());
}
});
}
我建议测试使用类似[邮差(HTTPS的API:// WWW .getpostman.com /)查看返回的响应类型。 API听起来像它没有返回json。 – mononz
只需检查一次回复。有时候由于错误,你会得到html格式的响应。 –
原来,API实际上是一个配置为返回JSON的SOAP API。我曾使用过Retrofit,被告知它是一个REST API。因此我改变了我的代码,它正在工作。感谢大家。 –