2016-11-07 90 views
0

我得到了錯誤的請求400當我發送base64圖像在web服務, 我把base64字符串JSON和POST使用改進。這裏是我的代碼200kb圖像base64無法發送到網絡服務

Bitmap bm = BitmapFactory.decodeFile("IMAGE PATH"); 
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
      bm.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream); 
      byte[] byteArray = byteArrayOutputStream .toByteArray(); 
      String imgbase64 = Base64.encodeToString(byteArray, Base64.NO_WRAP + Base64.URL_SAFE); 

,並加入到JSONObject的

JSONObject jo = new JSONObject(); 
      try { 
       jo.put("id",users.get(counter).getId()); 
       jo.put("firstname",users.get(counter).getFirstname()); 
       jo.put("middlename",users.get(counter).getMiddlename()); 
       jo.put("lastname",users.get(counter).getLastname()); 
       jo.put("suffix",users.get(counter).getSuffix()); 
       jo.put("image",imgbase64); 
      jo.put("date_registered",users.get(counter).getDate_registered()); 


      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

而且我會用改裝發送

ServiceInterface si = RestClient.getClient(); 
Observable<Response> call = si.postUser(jo.toString()); 

圖像大小爲200KB,我得到一個錯誤錯誤的請求 但是當我的圖像大小隻有10kb,它工作正常。 請幫忙。

+0

用於大尺寸圖像的base64字符串可能非常長。這就是爲什麼有時它們只能發送到api的大小。更好的辦法是使用多部分上傳文件 –

+0

webservice如何接收多部分參數?我的web服務只接受JSON? –

+0

你也必須修改你的webservice –

回答

0

嘗試使用以下解決方案,解碼圖像和發送

Bitmap bitmap; 
    String encodedString =""; 
    String filepath=""; // your uploaded image file path 

    try { 
      BitmapFactory.Options options = null; 
      options = new BitmapFactory.Options(); 
      options.inSampleSize = 1; 
      bitmap = BitmapFactory.decodeFile(filepath, options); 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      // Must compress the Image to reduce image size to make upload easy 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 

      byte[] byte_arr = stream.toByteArray(); 
      // Encode Image to String 
      encodedString = Base64.encodeToString(byte_arr, 0); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

你可以把encodedString到您的JSON。

這裏是用於解碼和上傳映像到服務器的PHP web服務器,也可以執行您對數據庫的操作。

<?php 

    include('../db/config.php'); 

    // Get image string posted from Android App 
    $base=$_REQUEST['image']; 
    // Get file name posted from Android App 
    $filename = $_REQUEST['filename'];// this your image filename like testimage.jpg 

    //other files 
    $id= $_REQUEST['id']; 
    $firstname = $_REQUEST['firstname']; 
    $middlename = $_REQUEST['middlename'];// add your parameter 

    // Decode Image 
    $binary=base64_decode($base); 
    header('Content-Type: bitmap; charset=utf-8'); 
    // Images will be saved under 'www/imgupload/uplodedimages' folder 
    $file = fopen('uploads/'.$filename, 'wb'); 
    // Create File 
    fwrite($file, $binary); 
    fclose($file); 
    // echo 'Image upload complete, Please check your php file directory'; 
    if(isset($filename) && $filename !=''){ 
     $fileUrl = "http://yourdomain/android/upload/".$filename; 
     }else{ 
     $fileUrl = ''; 
     } 
    $sql=mysql_query("INSERT INTO tbl_name(Id,firstName,middleName,FileUrl) VALUES ('$Id','$firstName','$middleName','$fileUrl')"); 

    ?> 

我建議嘗試使用這個解決方案,它會工作。

+0

仍然我得到了HTTP 400 BAD REQUEST –

+0

檢查您的網絡服務的URL和修改你的web服務,粘貼web服務的代碼 –

+0

我的web服務參數是JSON, –