这是android app中的截击请求代码
String url = "http://xx.xx.xx.xx:80/api/attendances"; //this is address. replaced with xx here
JSONArray jsonArray = new JSONArray();
for(int i=0; i < studentList.size(); i++)
{
jsonArray.put(studentList.get(i).toJson());
}
JSONObject parameters = new JSONObject();
try {
parameters.put("class_id", class_id);
parameters.put("students", jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
url, parameters,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("API", response.toString());
try {
if(response.getString("status").equalsIgnoreCase("Success")){
}
else {
Toast.makeText(getBaseContext(), response.getString("msg"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// hide the progress dialog
VolleyLog.d("API", "Error: " + error.toString());
Toast.makeText(getBaseContext(), "Unable to contact server.", Toast.LENGTH_LONG).show();
if(error.networkResponse.data!=null) {
VolleyLog.d("API", "Error: " + body);
}
pDialog.dismiss();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization","Bearer "+ token);
return headers;
}
};
AppController.getInstance().addToRequestQueue(jsonObjectRequest, tag_json_obj);
这会产生jsonObjectRequest
{"class_id":"11","students":[{"id":"222","present":true},{"id":"223","present":true},{"id":"224","present":false}]}
此请求的Laravel代码是 -
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'students' => 'required',
'class_id' => 'required',
]);
if ($validator->fails()) {
return response()->json($validator->errors());
}
$user = auth()->user();
if (Gate::allows('post_attendance')) {
$attendance = Attendance::create([
'cl_id' => $request->class_id,
'user_id' => $user->id,
]);
try {
$students = json_decode($request->students);
foreach ($students as $student) {
Present::create([
'attendance_id' => $attendance->id,
'student_id' => $student->id,
'present' => $student->present
]);
}
} catch (\Exception $exception) {
return response()->json(['error', $exception->getMessage()], 300);
}
return response()->json([
'status' => 'Success',
'msg' => 'Added'
]);
}
return response()->json([
'status' => 'Error',
'msg' => 'Access Denied!'
]);
}
如果我使用Android应用程序,此代码会在Attendance表中创建一个条目但仍会出现错误“BasicNetwork.performRequest:http://XX.XX.XX.XX:80/api/attendances的意外响应代码300”。所以解析JSONArray时遇到了问题。如果我注释掉try catch part它会成功运行状态为'Success'。
此外,如果使用API客户端相同的Laravel代码可以正常工作,并在考勤表和演示表中创建条目。
在屏幕截图中,学生字段包含以下给出的学生jsonarray -
[{"id":"222","present":true},{"id":"223","present":true},{"id":"224","present":false}]
问题是我不知道为什么从API客户端发送的请求正在工作,但是同样的请求不能在Android客户端上运行,Laravel代码与API客户端完美配合。但它确实适用于从android发送的请求。
我搜索了很多未发现任何类似问题或调试android客户端的方法。 Volley仅提供错误响应代码。有没有办法访问Laravel给出的整个错误响应? 任何帮助表示赞赏
答案 0 :(得分:1)
您正在发送json
,您可以像json那样获得
try {
$students = json_decode($request->getContent());
foreach ($students['students'] as $student) {
Present::create([
'attendance_id' => $attendance->id,
'student_id' => $student->id,
'present' => $student->present
]);
}
} catch (\Exception $exception) {
return response()->json(['error', $exception->getMessage()], 300);
}
使用$request->getContents()
获取json
希望这有帮助
这有助于进行这些调整
try {
$data = json_decode($request->getContent(),true);
$attendance = Attendance::create([
'cl_id' => $data['class_id'],
'user_id' => $user->id,
]);
foreach ($data['students'] as $student) {
Present::create([
'attendance_id' => $attendance->id,
'student_id' => $student['id'],
'present' => $student['present']
]);
}
} catch (\Exception $exception) {
return response()->json(['error', $exception->getMessage()], 300);
}