在這下面的代碼我使用RetroFit2爲對象發佈到端點:http://localhost:3000/insert:
改造響應參數之後的後
Call<DeviceModel> deviceModelCall = api.createDevice(device);
deviceModelCall.enqueue(new Callback<DeviceModel>() {
@Override
public void onResponse(Call<DeviceModel> call, Response<DeviceModel>
response) {
//How do I get access to the {"success" : true} object sent as a response form the endpoint after I posted.
}
@Override
public void onFailure(Call<DeviceModel> call, Throwable t) {
Log.d("Failure", "ON FAILURE" + "Failure");
}
});
現在在我的節點API如果保存的對象是成功的我返回JSON對象:{"success" : true}
。
但在上面的onResponse
方法中response
變量的參數類型爲Response<DeviceModel>
。如何從onResponse()
的上述方法中提取我從下面的節點API發回的{"success" : true}
對象?有沒有辦法做到這一點?
router.post('/insert', function(req, res) {
//Create Object
var obj = new Device({
});
obj.save(function(err) {
if (err) {
console.log("SAVE NOT SUCCESSFUL");
}else {
console.log("SAVE SUCCESS");
res.json({
"success" : true
});
}
});
});
如果響應不是'DeviceModel',爲什麼要使用'Call'? –
njzk2