2017-02-14 151 views
0

我一直在用这段代码挣扎好几个小时。有人可以指导我在哪里我错过了?如果上传一个文件,它是成功的。当我将代码更改为多个文件时,它将会出错。 这里是我的okhttp JSONParser.java用okhttp3上传多个文件

public static JSONObject uploadImage(ArrayList<String> photoCaptions) { 


    try { 

      String photoUrl = photoCaptions.get(0); 
      String photoUrl2 = photoCaptions.get(1); 
      String photoUrl3 = photoCaptions.get(2); 

      File sourceFile = new File(photoUrl); 
      File sourceFile2 = new File(photoUrl2); 
      File sourceFile3 = new File(photoUrl3); 

      final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/*"); 

      String filename = photoUrl.substring(photoUrl.lastIndexOf("/")+1); 
      String filename2 = photoUrl2.substring(photoUrl2.lastIndexOf("/")+1); 
      String filename3 = photoUrl2.substring(photoUrl2.lastIndexOf("/")+1); 

      Log.d("TAG", "File...::::" + sourceFile.getName() + " : "+ photoUrl + " - " + sourceFile.exists()); 


     MultipartBody.Builder multipartBuilder = new MultipartBody.Builder() 
       .setType(MultipartBody.FORM); 

     RequestBody requestBody = multipartBuilder 
       .setType(MultipartBody.FORM) 
       .addFormDataPart("image1", filename, RequestBody.create(MEDIA_TYPE_PNG, sourceFile)) 
       .addFormDataPart("image2", filename2, RequestBody.create(MEDIA_TYPE_PNG, sourceFile2)) 
       .addFormDataPart("image3", filename3, RequestBody.create(MEDIA_TYPE_PNG, sourceFile3)) 

       .build(); 

     Request request = new Request.Builder() 
       .url(URL_UPLOAD_IMAGE) 
       .post(requestBody) 
       .build(); 

     OkHttpClient client = new OkHttpClient(); 
     Response response = client.newCall(request).execute(); 
     String res = response.body().string(); 
     Log.e("TAG", "Error: " + res); 
     return new JSONObject(res); 



    } catch (UnknownHostException | UnsupportedEncodingException e) { 
     Log.e("TAG", "Error: " + e.getLocalizedMessage()); 
    } catch (Exception e) { 
     Log.e("TAG", "Other Error: " + e.getLocalizedMessage()); 
    } 
    return null; 
} 

以下是错误

02-15 02:15:20.425 27709-27821/com.domain.appgallery E/TAG: Other Error: End of input at character 0 of 

这是我的PHP代码

<?php 

//importing dbDetails file 
require_once 'dbDetails.php'; 

//this is our upload folder 
$upload_path = 'uploads/'; 

//Getting the server ip 
$server_ip = gethostbyname(gethostname()); 

//creating the upload url 
$upload_url = 'http://'.$server_ip.'/gallery/'.$upload_path; 

//response array 
$response = array(); 


if($_SERVER['REQUEST_METHOD']=='POST'){ 

    //checking the required parameters from the request 
    if(isset($_FILES['image']['name'])){ 

     //connecting to the database 
     $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect...'); 

     //getting name from the request 
     //$name = $_POST['name']; 

     //getting file info from the request 
     $fileinfo = pathinfo($_FILES['image']['name']); 

     //getting the file extension 
     $extension = $fileinfo['extension']; 

     //file url to store in the database 
     $file_url = $upload_url . getFileName() . '.' . $extension; 

     //file path to upload in the server 
     $file_path = $upload_path . getFileName() . '.'. $extension; 

     //trying to save the file in the directory 
     try{ 
      //saving the file 
      move_uploaded_file($_FILES['image']['tmp_name'],$file_path); 
      $sql = "INSERT INTO `photos` (`id`, `url`, `name`) VALUES (NULL, '$file_url', NULL);"; 

      //adding the path and name to database 
      if(mysqli_query($con,$sql)){ 

       //filling response array with values 
       $response['error'] = false; 
       $response['url'] = $file_url; 
       //$response['name'] = $name; 
       $response['result'] = 'success'; 

      } 
     //if some error occurred 
     }catch(Exception $e){ 
      $response['error']=true; 
      $response['message']=$e->getMessage(); 
     }  
     //displaying the response 
     echo json_encode($response); 

     //closing the connection 
     mysqli_close($con); 
    }else{ 
     $response['error']=true; 
     $response['message']='Please choose a file'; 
    } 
} 

/* 
    We are generating the file name 
    so this method will return a file name for the image to be upload 
*/ 
function getFileName(){ 
    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect...'); 
    $sql = "SELECT max(id) as id FROM photos"; 
    $result = mysqli_fetch_array(mysqli_query($con,$sql)); 

    mysqli_close($con); 
    if($result['id']==null) 
     return 1; 
    else 
     return ++$result['id']; 
} 

回答

0

这是我不好。 我没有看php代码。 用于循环处理图像。 并在android中改变某种类型。

现在,它的工作。