2017-02-14 8 views
0

私はこのコードを何時間も苦労してきました。誰かが私を見逃したところで私を導くことができる? 1つのファイルをアップロードすると成功です。コードを複数のファイルに変更するとエラーになります。ここで は私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コードを一切見ていません。 イメージを処理するforループを追加しました。 アンドロイドでいくつかのタイプを変更します。

これで機能します。

関連する問題