Retrofit调用Azure认知服务API异常:无响应数据且无法发送Raw JSON
解决Retrofit发送原始JSON及响应数据缺失问题
一、无法发送原始JSON的解决方案
要让Retrofit发送原始JSON请求体,你需要正确构建RequestBody并调整接口方法定义,具体步骤如下:
修改ApiInterface的请求方法
把接口方法改成接收RequestBody类型的参数,示例:public interface ApiInterface { @POST("your-api-endpoint-path") // 替换为实际API路径,比如对应文档里的接口路径 Call<YourResponseModel> createProfile(@Body RequestBody requestBody); // 若暂时不确定响应结构,可先返回ResponseBody查看原始响应内容 @POST("your-api-endpoint-path") Call<ResponseBody> createProfileRaw(@Body RequestBody requestBody); }构建原始JSON请求体
用JSONObject拼接参数后,转换为指定MediaType的RequestBody:try { JSONObject paramObject = new JSONObject(); paramObject.put("locale", "en-us"); // 补充其他需要的参数 // paramObject.put("otherField", "correspondingValue"); RequestBody requestBody = RequestBody.create( MediaType.parse("application/json; charset=utf-8"), paramObject.toString() ); // 调用API ApiInterface apiService = ApiClient.getClientRequest().create(ApiInterface.class); Call<YourResponseModel> call = apiService.createProfile(requestBody); call.enqueue(new Callback<YourResponseModel>() { // 后续处理响应逻辑... }); } catch (JSONException e) { e.printStackTrace(); }确保Retrofit客户端配置正确
检查ApiClient是否添加了必要的转换器,比如GsonConverterFactory用于解析JSON响应,若需处理字符串或原始响应,可额外添加ScalarsConverterFactory:public class ApiClient { public static Retrofit getClientRequest() { return new Retrofit.Builder() .baseUrl("https://westus.dev.cognitive.microsoft.com/") .addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(ScalarsConverterFactory.create()) // 可选,用于处理字符串响应 .client(new OkHttpClient.Builder().build()) .build(); } }
二、Retrofit响应不包含数据的排查与解决
响应无数据通常是解析不匹配或请求未通过验证导致的,按以下步骤排查:
先查看原始响应内容
暂时改用Call<ResponseBody>获取原始响应,确认API实际返回的内容:apiService.createProfileRaw(requestBody).enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if (response.isSuccessful()) { try { Log.d("Raw Response", response.body().string()); } catch (IOException e) { e.printStackTrace(); } } else { try { Log.e("Error Response", response.errorBody().string()); } catch (IOException e) { e.printStackTrace(); } } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { t.printStackTrace(); } });这样你就能明确API是返回了错误(比如401未授权)还是JSON结构与你的模型不匹配。
检查API认证信息
你调用的Azure认知服务API需要在请求头中添加Ocp-Apim-Subscription-Key,记得在接口方法中添加这个头部:@POST("your-api-endpoint-path") Call<YourResponseModel> createProfile( @Header("Ocp-Apim-Subscription-Key") String subscriptionKey, @Body RequestBody requestBody );调用时传入你的订阅密钥即可:
Call<YourResponseModel> call = apiService.createProfile("your-subscription-key", requestBody);确保响应模型与API返回结构匹配
根据API返回的JSON结构调整你的响应模型类,比如API返回:{ "id": "profile-id-123", "locale": "en-us", "createdDateTime": "2024-05-20T10:00:00Z" }对应的模型类应为:
public class YourResponseModel { private String id; private String locale; @SerializedName("createdDateTime") private String createdDateTime; // 生成getter和setter方法 public String getId() { return id; } public void setId(String id) { this.id = id; } // 其他字段的getter/setter }注意字段名要与JSON对应,或用
@SerializedName映射不同的命名规则。
内容的提问来源于stack exchange,提问作者ANDROID DEVELOPER
相关产品推荐
相关产品推荐

