【发布时间】:2020-11-11 14:47:10
【问题描述】:
我有如下服务。我将方法称为Retroifit 库。当我发送带有@Body Test 类型的参数但我想使用@Body String 我不创建Test 类并在@Body 中使用它时,该服务有效。我创建了一个JSONObject 并将其转换为String,但是这样,程序就不起作用了!您能帮我或提出解决方案吗?
我的网络 API:
[Route("TestService"), HttpPost, IgnoreDataLog]
public async Task<Result<TestResult>> Add(Test pmDeviceObj)
{
var listResult = await pmService.AddAsync(pmDeviceObj);
return listResult;
}
安卓部分:
@POST("TestService")
Call<Result<TestResult>> TestService(@Header("Content-Type") String content_type,@Body String body);
在 Android 中调用服务-> 我使用以下代码得到 StatusCode 400
JSONObject jsonBody=new JSONObject();
try {
jsonBody.put("Id",73);
jsonBody.put("seri","55656573");
jsonBody.put("code","fc24009b9160");
jsonBody.put("sID",8);
}catch (JSONException ex){
ex.printStackTrace();
}
retrofit2.Call<Result<TestResult>> call1=service.TestService("application/json",jsonBody.toString());
如果我在 Android 部分使用以下代码,一切正常,我会获取数据。
@POST("TestService")
Call<Result<TestResult>> TestService(@Header("Content-Type") String content_type,@Body Test inputValue);
Test test=new Test(73,"556565","fc24009b9160",8);
retrofit2.Call<Result<TestResult>> call1=service.TestService("application/json",test);
【问题讨论】:
标签: java android retrofit converters webapi