我正在向返回 JSON 数据的路由发出 POST 请求。
[HttpPost("api/v1/testGetAll")]
public object Test([FromBody]object filteringOptions)
{
return myService.GetLogs(filteringOptions).ToArray();
}
Run Code Online (Sandbox Code Playgroud)
路由工作正常,过滤工作正常,当我在 Postman 中测试路由时,我得到了正确的响应。然而,这只是一个后端,我想从我的自定义 API 网关调用这个路由。
我面临的问题是得到准确的回应。相反,我正在获取成功状态、标题、版本、请求消息等。
public object TestGetAll(string ApiRoute, T json)
{
Task<HttpResponseMessage> response;
var url = ApiHome + ApiRoute;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
try
{
response = client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, "application/json"));
return response.Result;
}
catch (Exception e)
{
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得准确的内容?
我已经制作了一个REST服务,除了删除之外,它可以完美地完成所有CRUD操作.当我处理删除请求时,一切都很好,直到最后一个元素ArrayList<User>.当我删除最后一个时,由于某种原因我得到500内部服务器错误?当我将构造函数放在UserList类中创建默认的2个用户时,它会成功创建它们,并且在我执行删除请求后,我可以删除一个用户.当我尝试删除剩下的最后一个时,我得到了500错误,当我尝试列出所有创建的用户时,不知怎的,构造函数再次被调用,我再次拥有这2个默认用户?
删除 - 服务器端
@DELETE
@Path("/delete")
@Consumes("application/json")
public Response deleteUser(String input) {
JSONParser parser = new JSONParser();
Object obj = null;
try {
obj = parser.parse(input);
} catch (ParseException e) {
e.printStackTrace();
}
JSONObject jsonObject = (JSONObject) obj;
String uName = (String) jsonObject.get("userName");
try {
uName = URLDecoder.decode(uName, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
returnAll = usrList.getUsers();
if (userExists(uName)) {
for (User u : returnAll) {
if (u.getUserName().equals(uName)) {
returnAll.remove(u);
}
}
return Response.status(204).entity("User …Run Code Online (Sandbox Code Playgroud) 我有一个键值对字典.如何检查where子句失败?
SomeDictionary.Where(x => x.Value && someOtherBool).First();
Run Code Online (Sandbox Code Playgroud)
有没有办法检查这是否返回了一些结果而没有包围try-catch块?当然,我可以打电话FirstOrDefault()但是我得不到我需要的东西......
真的应该有一种方法FirstOrReturn(arg)......
有没有办法运行Timer它,以便它完全开始?
stateTimer = new Timer(someCallback, null, 0, 1000);
Run Code Online (Sandbox Code Playgroud)
这个将立即开始,重复每一秒,但问题是它将在我运行可能导致13:14:15.230启动的程序时正好启动.
我想13:14:15.000开始.就像是:
stateTimer = new Timer(someCallback, null, DateTime.Now.Date, 1000);
Run Code Online (Sandbox Code Playgroud)
那可能吗?
编辑:
执行控制台日志后:
Console.WriteLine($"Time: {DateTime.Now.ToString("HH:mm:ss.fff")}");
Run Code Online (Sandbox Code Playgroud)
我注意到1秒间隔实际上增加了一秒以上(每次迭代约1,02次),因此在50次迭代后会跳过一秒.我已经解决了我的问题,让计时器每800毫秒运行一次.不理想的解决方案,但它的工作原理,第二个永远不会被跳过(我在触发相同的秒两次没有问题).
stateTimer = new Timer(someCallback, null, 0, 800);
Run Code Online (Sandbox Code Playgroud) 有没有办法将Laravel Collective日期选择器格式设置为yyyy-mm-dd?
我现在正在使用:
{{ Form::date('deadline', null,['class' => 'form-control']) }}
Run Code Online (Sandbox Code Playgroud)
但在前端,我得到了一个带有mm/dd/yyyy内部的输入字段。我尝试将Carbon实例解析为第二个参数,但这没有任何作用。
{{ Form::date('deadline', \Carbon\Carbon::now()->format('Y-m-d'),['class' => 'form-control']) }}
Run Code Online (Sandbox Code Playgroud) 所以我通过POST请求从Ionic应用程序发送到API,一系列调味品在移动部件上表示为复选框.我想获取已选中的复选框,以便我可以相应地创建表格行.
在路由触发时调用的方法有以下部分:
$condiments = Input::only('condiments');
foreach ($condiments as $condiment) {
if ($condiment['checked'] == 1) {
OrderCondiment::create(['order_item_id' => $orderItem,
'condiment_id' => $condiment->id]);
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到Illegal string offset 'checked'错误.我尝试过,$condiment->checked但后来我得到了Trying to get the property of non-object错误...我错过了什么...除了咖啡
编辑
dd($condiments)
array:1 [
"condiments" => "[{"id":1,"name":"ut","price":3,"created_at":null,"updated_at":null,"checked":1},{"id":2,"name":"ipsam","price":5,"created_at":null,"updated_at":null,"checked":1},{"id":3,"name":"dolores","price":10,"created_at":null,"updated_at":null,"checked":1},{"id":4,"name":"esse","price":3,"created_at":null,"updated_at":null,"checked":0},{"id":5,"name":"aliquid","price":1,"created_at":null,"updated_at":null,"checked":0},{"id":6,"name":"sunt","price":3,"created_at":null,"updated_at":null,"checked":0},{"id":7,"name":"saepe","price":1,"created_at":null,"updated_at":null,"checked":0},{"id":8,"name":"impedit","price":10,"created_at":null,"updated_at":null,"checked":0},{"id":9,"name":"dolores","price":4,"created_at":null,"updated_at":null,"checked":0},{"id":10,"name":"veniam","price":2,"created_at":null,"updated_at":null,"checked":0}]"
]
Run Code Online (Sandbox Code Playgroud) 我在掌握任务和取消令牌方面遇到了一些问题.我做了一个看起来像这样的程序:
static void Main(string[] args)
{
CancellationTokenSource token = new CancellationTokenSource();
Stopwatch stop = new Stopwatch();
stop.Start();
for (int i = 0; i < 5; i++)
{
//Thread.Sleep(1000);
Task.Factory.StartNew(() => myLongTask(token.Token, (i + 1) * 1000));
}
while (true)
{
Thread.SpinWait(1000);
if (stop.ElapsedMilliseconds > 3000)
{
token.Cancel();
}
}
}
public static void myLongTask(CancellationToken token, int time)
{
if (token.IsCancellationRequested)
{
Console.WriteLine("Cancelled");
return;
}
var sw = Stopwatch.StartNew();
Console.WriteLine($"Task {time / 1000} started");
while (sw.ElapsedMilliseconds < time)
Thread.SpinWait(1000);
Console.WriteLine($"Task {time …Run Code Online (Sandbox Code Playgroud) c# ×5
laravel ×2
.net ×1
api ×1
arrays ×1
asynchronous ×1
date ×1
dictionary ×1
foreach ×1
java ×1
json ×1
laravel-5.4 ×1
performance ×1
rest ×1
server ×1
service ×1
task ×1
timer ×1