这个问题以前可能被问过,但没有明确的答案。如何在Retrofit请求的主体中发布原始的整个JSON ?
在这里看到类似的问题。或者这个答案是正确的,它必须是表单url编码并作为一个字段传递?我真的希望不是,因为我要连接的服务只是希望在文章正文中有原始JSON。它们不是用来为JSON数据寻找特定字段的。
I just want to clarify this with the restperts once and for all. One person answered not to use Retrofit. The other was not certain of the syntax. Another thinks yes it can be done but only if its form url-encoded and placed in a field (that's not acceptable in my case). No, I can't re-code all the services for my Android client. And yes, it's very common in major projects to post raw JSON instead of passing over JSON content as field property values. Let's get it right and move on. Can someone point to the documentation or example that shows how this is done? Or provide a valid reason why it can/should not be done.
更新:有一件事我可以百分之百确定。你可以在谷歌的Volley中做到这一点。这是与生俱来的。我们可以在Retrofit中这样做吗?
我发现当你使用一个复合对象作为@Body参数时,它不能很好地与Retrofit的GSONConverter一起工作(假设你正在使用它)。
你必须使用JsonObject,而不是JsonObject,它添加了NameValueParams而没有详细说明它-你只能看到如果你添加了日志拦截器的另一个依赖项,和其他诡计。
所以我发现解决这个问题的最佳方法是使用RequestBody。
您可以通过一个简单的api调用将对象转换为RequestBody并启动它。
在我的情况下,我转换一个地图:
val map = HashMap<String, Any>()
map["orderType"] = orderType
map["optionType"] = optionType
map["baseAmount"] = baseAmount.toString()
map["openSpotRate"] = openSpotRate.toString()
map["premiumAmount"] = premiumAmount.toString()
map["premiumAmountAbc"] = premiumAmountAbc.toString()
map["conversionSpotRate"] = (premiumAmountAbc / premiumAmount).toString()
return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSONObject(map).toString())
这就是号召:
@POST("openUsvDeal")
fun openUsvDeal(
@Body params: RequestBody,
@Query("timestamp") timeStamp: Long,
@Query("appid") appid: String = Constants.APP_ID,
): Call<JsonObject>
在Retrofit2中,当你想要发送原始参数时,你必须使用标量。
首先在gradle中添加这个:
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:converter-scalars:2.3.0'
你的界面
public interface ApiInterface {
String URL_BASE = "http://10.157.102.22/rest/";
@Headers("Content-Type: application/json")
@POST("login")
Call<User> getUser(@Body String body);
}
活动
public class SampleActivity extends AppCompatActivity implements Callback<User> {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiInterface.URL_BASE)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
// prepare call in Retrofit 2.0
try {
JSONObject paramObject = new JSONObject();
paramObject.put("email", "sample@gmail.com");
paramObject.put("pass", "4384984938943");
Call<User> userCall = apiInterface.getUser(paramObject.toString());
userCall.enqueue(this);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onResponse(Call<User> call, Response<User> response) {
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
}
你需要在interface中设置@Body
@Headers({ "Content-Type: application/json;charset=UTF-8"})
@POST("Auth/Login")
Call<ApiResponse> loginWithPhone(@Body HashMap<String, String> fields);
要通过生身改装就用:
HashMap<String,String> SendData =new HashMap<>();
SendData.put("countryCode",ccode);
SendData.put("phoneNumber",phone);
Call<ApiResponse>call = serviceInterface.loginWithPhone(SendData);
这对我来说很管用:
使用JsonObject是它的方式:
Create your interface like this:
public interface laInterfaz{
@POST("/bleh/blah/org")
void registerPayer(@Body JsonObject bean, Callback<JsonObject> callback);
}
Make the JsonObject acording to the jsons structure.
JsonObject obj = new JsonObject();
JsonObject payerReg = new JsonObject();
payerReg.addProperty("crc","aas22");
payerReg.addProperty("payerDevManufacturer","Samsung");
obj.add("payerReg",payerReg);
/*json/*
{"payerReg":{"crc":"aas22","payerDevManufacturer":"Samsung"}}
/*json*/
Call the service:
service.registerPayer(obj, callBackRegistraPagador);
Callback<JsonObject> callBackRegistraPagador = new Callback<JsonObject>(){
public void success(JsonObject object, Response response){
System.out.println(object.toString());
}
public void failure(RetrofitError retrofitError){
System.out.println(retrofitError.toString());
}
};
这就是它!在我个人看来,这比制作pojo和处理班级混乱要好得多。这样干净多了。