【发布时间】:2020-03-24 11:02:25
【问题描述】:
我正在 Laravel 中开发一个 API,它输出 JSON 供 Android 应用使用。我在 Laravel 方面工作,我的朋友在 Android 方面工作。我们可以将 Android 应用程序连接到 API,但由于某种原因它无法访问数据。我们认为我们遇到的问题是,Laravel 的所有 JSON 输出都在一个数据标签中,Android 应用无法访问该标签。
所有文章的代码
public function toArray($request)
{
return [
'status' => 'ok',
'totalResults' => count($this),
'articles' => ArticleResource::collection($this->collection),
];
}
一篇文章的代码
public function toArray($request)
{
return [
'source' => [
'id' => $this->id,
'name' => $this->name
],
'author' => null,
'title' => $this->title,
'description' => $this->description,
'url' => $this->url,
'urlToImage' => null,
'publishedAt' => $this->created_at,
'content' => null
//]
];
}
我一直在寻找它是如何以及为什么都在数据标签中的,或者这就是我们遇到问题的原因,但我什至几乎找不到 Laravel 在数据标签中输出 JSON 的其他示例,实际上没关系修复它。有谁知道我该如何解决这个问题?如果您需要更多信息/代码/屏幕截图,请告诉我,谢谢。
编辑: Also, as well as all the articles, nested in the data tag is the pagination
中间件代码:
验证.php
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
TrimStrings.php
protected $except = [
'password',
'password_confirmation',
];
TrustProxies.php
protected $proxies;
protected $headers = Request::HEADER_X_FORWARDED_ALL;
验证CsrfToken.php
protected $addHttpCookie = true;
protected $except = [
(中间件代码结束)
Not sure if this will be helpful, every occurrence of the word 'data' in my project
【问题讨论】:
-
您如何访问 API 响应?它应该可以通过
response.data获得 -
似乎你有一些改变响应的中间件。检查启用的中间件。
-
在客户端尝试解析 json,因为 json 是以字符串格式从一个点发送到另一个点,所以在访问数据标签之前您可能不会将其转换为 json
-
蒂姆刘易斯,我不完全确定你的意思,我在谷歌浏览器或邮递员上检查我的 API 输出
-
Jagad89,我刚刚在编辑中添加了我的中间件代码,目前还有另外两个中间件文件具有空函数。
标签: php android json laravel api