我无法理解如何使用 POST 数据将图像文件从我的 android 设备上传到 Laravel API,我已经使用 android-asynchronous-http-client 向 API 发送了一些文本数据,但它不适用于图像文件。API 端工作正常我使用 POSTMAN 对其进行了测试,但在 android 设备上出现错误status code: 500 Internal Server Error
安卓部分
RequestParams ComplaintsDataRequestParams = new RequestParams();
ComplaintsDataRequestParams.put("Complaint_Subject", complaintsSubject.getText().toString());
ComplaintsDataRequestParams.put("Complaint_Details", complaintsDetails.getText().toString());
ComplaintsDataRequestParams.put("Complaint_Address", complaintAddress.getText().toString());
ComplaintsDataRequestParams.put("Username", HomeActivity.home_username);
ComplaintsDataRequestParams.put("Complainant_CNIC", HomeActivity.user_cnic);
ComplaintsDataRequestParams.put("Complaint_id", uniqueComplaintID);
ComplaintsDataRequestParams.put("User_Location", user_complete_address + "-" + lat + "," + lng);
if (ImageEvidence1 != null) {
evidenceID1 = uniqueComplaintID + "-" + System.currentTimeMillis();
ComplaintsDataRequestParams.put("Evidence_ID1", "YES");
ComplaintsDataRequestParams.put("Evidence_ID1_name", evidenceID1);
ComplaintsDataRequestParams.put("Evidence_ID1_data", ImageEvidence1);
}
if (ImageEvidence2 != null) {
evidenceID2 = uniqueComplaintID + "-" + System.currentTimeMillis();
ComplaintsDataRequestParams.put("Evidence_ID2", "YES");
ComplaintsDataRequestParams.put("Evidence_ID2_name", evidenceID2);
ComplaintsDataRequestParams.put("Evidence_ID2_data", ImageEvidence2);
}
if (ImageEvidence3 != null) {
evidenceID3 = uniqueComplaintID + "-" + System.currentTimeMillis();
ComplaintsDataRequestParams.put("Evidence_ID3", "YES");
ComplaintsDataRequestParams.put("Evidence_ID3_name", evidenceID3);
ComplaintsDataRequestParams.put("Evidence_ID3_data", ImageEvidence3);
}
Complaints_REST_API_Client.POST("?action=store", ComplaintsDataRequestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
// called when response HTTP status is "200 OK"
complaint_confirmation();
Log.d("newComplaintActivity", "statusCode " + statusCode);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable error) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
submittingComplaint.dismiss();
Log.d("newComplaintActivity", "statusCode " + statusCode);
Toast.makeText(getContext(), "Exception: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
这ImageEvidence
是一个Uri
类型变量,用于imageView.setImageURI(ImageEvidence)
并且evidenceID
只是图像文件的名称。
LARAVEL API 部分
Complaint::create([
'title' => $request->input('Complaint_Subject'),
'description' => $request->input('Complaint_Details'),
'address' => $request->input('Complaint_Address'),
'firebase_user_id' => $request->input('Username'),
'firebase_user_cnic' => $request->input('Complainant_CNIC'),
'firebase_complaint_id' => $request->input('Complaint_id'),
'firebase_user_location' => $request->input('User_Location'),
'status' => 'pending'
]);
if ($request->input('Evidence_ID1') == "YES")
Evidence::create([
'evidence_name' => $request->input('Evidence_ID1_name'),
'eivdence_image' => $request->file('Evidence_ID1_data')->store('public/evidences'),
'complaint_id' => $request->input('Complaint_id')
]);
if ($request->input('Evidence_ID2') == "YES")
Evidence::create([
'evidence_name' => $request->input('Evidence_ID2_name'),
'eivdence_image' => $request->file('Evidence_ID2_data')->store('public/evidences'),
'complaint_id' => $request->input('Complaint_id')
]);
if ($request->input('Evidence_ID3') == "YES")
Evidence::create([
'evidence_name' => $request->input('Evidence_ID3_name'),
'eivdence_image' => $request->file('Evidence_ID3_data')->store('public/evidences'),
'complaint_id' => $request->input('Complaint_id')
]);
请告诉我应该使用哪个库来发送图像文件和 POST 数据。