我正在从事一项活动,其中用户输入一些数据(整数和字符串值)并从图库中选择多个图像。 整个数据应发布在一个请求中,我使用的是php作为后端。 我收到响应失败。
这是我在Api.interface中拥有的接口方法...
@Multipart
@POST("AddKhataSale")
Call<Reciever> SaverPost(@Part("city") String City, @Part("party") String Client,
@Part("image_count") Integer ImageCount, @Part("bill_total") Integer BillTotal,
@Part("bill_date") String BillDate, @Part List<MultipartBody.Part> parts
);
从“活动”结果中获取图库中的所有图像
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
try {
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
imagesEncodedList = new ArrayList<String>();
if(data.getData()!=null){
Uri mImageUri=data.getData();
Cursor cursor = getContentResolver().query(mImageUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageEncoded = cursor.getString(columnIndex);
cursor.close();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (data.getClipData() != null) {
ClipData mClipData = data.getClipData();
mArrayUri = new ArrayList<Uri>();
ImageCount = mClipData.getItemCount();
for (int i = 0; i < mClipData.getItemCount(); i++) {
ClipData.Item item = mClipData.getItemAt(i);
uri = item.getUri();
files[i] =new File(String.valueOf(uri));
mArrayUri.add(uri);
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageEncoded = cursor.getString(columnIndex);
imagesEncodedList.add(imageEncoded);
cursor.close();
}
Log.v("LOG_TAG", "Selected Images" + mArrayUri.size());
Toast.makeText(this,"Selected Images :"+mArrayUri.size(),Toast.LENGTH_SHORT).show();
}
}
}
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
super.onActivityResult(requestCode, resultCode, data);
}
在mArrayUri(Arraylist)中,我添加了图像的uri,s。
在按钮上载时,我已调用方法SendKhata()。
public void SendKhata()
{
try {
BillTotal = Integer.valueOf(billamount.getText().toString());
}
catch (Exception e)
{
Toast.makeText(this,"BillAmount must be Integer ",Toast.LENGTH_SHORT).show();
}
try {
BillDate = billdate.getText().toString();
}
catch (Exception e)
{
Toast.makeText(this,"BillAmount must be Integer ",Toast.LENGTH_SHORT).show();
}
ApiInterface = ApiClient.getApiClient().create(Api.class);
List<MultipartBody.Part> parts = new ArrayList<>();
for (int i=0; i < mArrayUri.size(); i++){
parts.add(prepareFilePart("image"+i+"", mArrayUri.get(i)));
}
//geting CityId,ClientName,ImageCount,BillTotal and BillDate here with no issues...
Call<Reciever> call = ApiInterface.SaverPost(CityID, selected_Client_Name, ImageCount, BillTotal, BillDate,
parts);
call.enqueue(new Callback<Reciever>() {
@Override
public void onResponse(Call<Reciever> call, Response<Reciever> response) {
if (response.isSuccessful()) {
Toast.makeText(EzKhataActivity.this, response.body().getCode() + " " + response.body().getMsg().toString()
, Toast.LENGTH_LONG).show();
Log.e("response:", "response successful");
}
}
@Override
public void onFailure(Call<Reciever> call, Throwable t) {
Log.e("response:", "response failiure");
}
});
}
private MultipartBody.Part prepareFilePart(String partName, Uri fileUri){
File file = new File(fileUri.getPath());
RequestBody requestBody = RequestBody.create(MediaType.parse(getContentResolver().getType(uri)), file);
return MultipartBody.Part.createFormData(partName, file.getName(),requestBody);
}
这是代码的php部分。
function AddKhataSale()
{
// params: city, client, no of images, images, total bill, bill date,
$CityId = $this->input->post('city');
$ClientId = $this->input->post('client');
$ImageCount = $this->input->post('image_count');
$BillTotal = $this->input->post('bill_total');
$BillDate = $this->input->post('bill_date');
$error = array();
for($i=0; $i<$ImageCount; $i++)
{
$FileName = $_FILES['image'.$i]['name'];
$FileTmp = $_FILES['image'.$i]['tmp_name'];
$FileType = $_FILES['image'.$i]['type'];
// die(__DIR__.'/../../uploads/');
$dp = getcwd().DIRECTORY_SEPARATOR;
$target_path = $dp . "aan/client_bills/".basename($FileName);
if(@move_uploaded_file($FileTmp, $target_path))
{
// echo "ok";
}
else
{
array_push($error, $FileName);
}
}
// echo $ImageCount;
if(count($error) > 0)
{
echo json_encode(array('code'=>'0', 'msg'=>$error));
}
else
{
echo json_encode(array('code'=>'1', 'msg'=>'successfully uploaded'));
}
}