2013-04-10 97 views
1

我在ASP.net中創建了一個RESTful的web服務。從Android應用程序我想要消費這些服務。基本上我使用Android的默認庫發送請求並獲得JSON響應。無法通過JSON發送base64字符串圖像到asp.net web服務android

這裏是我的代碼:

public class PostImagesHandler { 
    final static String TAG = "WebHandlerHttpPost"; 
    Context context; 
    ImagesBean imageBean; 

    public String postImagesRequest(String URL, Context context, 
      ImagesBean imgBean) { 
     this.context = context; 
     String result = null; 
     this.imageBean = imgBean; 

     try { 
      HttpPost request = new HttpPost(URL); 
      System.out.println("WHAT IS URL :: " + URL); 
      request.setHeader("Accept", "application/json"); 
      request.setHeader("Content-type", "application/json"); 
      request.addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. 
      request.addHeader("Pragma", "no-cache"); // HTTP 1.0. 
      request.addHeader("Expires", "0"); // Proxies. 


        Bitmap bm = BitmapFactory.decodeFile("sdcard/dotTest9.jpg"); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      bm.compress(Bitmap.CompressFormat.JPEG, 90, baos); 
      byte[] bytes_ = baos.toByteArray(); 

      String image_str = Base64.encodeToString(readFile().getBytes(), 
        Base64.NO_WRAP); 
      appendLog(image_str); 

      JSONStringer userJson = new JSONStringer().object() 
      .key("objPostSectionImages").object().key("ID") 
      .value("1").key("SUB_ID").value("1").key("MainTaskId") 
      .value("1").key("ImageBase64").value(image_str) 
      .key("actualAuditDate").value(getCurrentDateTime()).endObject() 
      endObject(); 


      Log.d(TAG, "SECTION IMAGE STRING :: " + userJson.toString()); 

      StringEntity entity = new StringEntity(userJson.toString(), "UTF-8"); 
      entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, 
        "application/json")); 
      entity.setContentType("application/json"); 
      request.setEntity(entity); 

      HttpParams params = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(params, 30000); 
      HttpConnectionParams.setSoTimeout(params, 10000); 

      // Send request to WCF service 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpResponse response = httpClient.execute(request); 

      if (response != null) { 
       StringBuilder sb = new StringBuilder(); 
       BufferedReader rd = new BufferedReader(new InputStreamReader(
         response.getEntity().getContent())); 

       String line; 
       while ((line = rd.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 

       result = sb.toString(); 
       Log.d(TAG, "AUDIT SECTION IMAGES Response: " + result); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return result; 
    } 

    public void appendLog(String text) { 
     File logFile = new File("sdcard/dotTest7.txt"); 
     if (!logFile.exists()) { 
      try { 
       logFile.createNewFile(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     try { 
      // BufferedWriter for performance, true to set append to file flag 
      BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, 
        true)); 
      buf.append(text); 
      // buf.newLine(); 
      buf.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    @SuppressLint("SimpleDateFormat") 
    public static String getCurrentDateTime() { 
     return new SimpleDateFormat("MM-dd-yyyy").format(Calendar.getInstance() 
       .getTime()); 
    } 
} 

我能夠這樣送小圖像,但同時發送大量圖像(超過500 KB),我面臨的問題。有時我會收到「請求錯誤」,有時會起作用。

+0

是否有任何理由不應該將圖像流式傳輸到服務而不是轉換爲Base64字符串? – 2013-04-10 13:31:04

+0

我正在創建一個json對象,並且base64是該對象的一個​​參數。然後將此對象傳遞給基於REST的WCF。 WCF需要base64字符串。 – Mohanish 2013-04-10 13:51:10

+0

你可以使用[multipart上傳](http://stackoverflow.com/questions/1354749/wcf-service-to-accept-a-post-encoded-multipart-form-data) – 2013-04-10 13:53:41

回答

0

也許您的ASP.NET webservice在base64字符串中轉義正斜槓?這使得這些數據從正常應用程序的角度來分解。

在小圖像中得到/符號後base64轉換的概率明顯低於大的一個,所以你得到這個隨機錯誤的情況。

您可以檢查您的服務產生的字符串爲/\符號,並將它們替換爲/並檢查它是否工作。

相關問題