2013-03-02 67 views
1

我有一個Android程序,可以上傳圖像到WCF服務。 WCF服務接受一個帶有圖像名稱,文件夾名稱和圖像的base64編碼的JSON對象。上傳base64編碼圖像到WCF服務從android

我使用fiddler來測試該服務,我從一個在線服務編碼圖像並將其放入對象中。然後組成一個POST請求;然後WCF服務成功上傳圖片。

但是,當我使用Android HTTP客戶端和做同樣的事情WCF接受請求,但我不認識的形象。

這裏是我的Android代碼:

try { 
    HttpPost httpPost=new HttpPost(this.remoteBasePath); 
    JSONObject obj = new JSONObject(); 
    obj.put(FILE_NAME, fileName); 
    obj.put(FILE_STREAM,fileStream); 
    obj.put(FOLDER_NAME, remoteFolder); 
    httpPost.setHeader("Content-type", "application/json; charset=utf-8"); 
    httpPost.setEntity(new StringEntity(obj.toString())); 
    httpPost.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
    httpResponse=httpClient.execute(httpPost); 
    if(httpResponse!=null){ 
     TLLog.d(TAG,"StatusCode : "+ httpResponse.getStatusLine().getStatusCode()); 
     if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {        
      InputStream instream = httpResponse.getEntity().getContent(); 
      BufferedReader r = new BufferedReader(
      new InputStreamReader(instream)); 
      StringBuilder total = new StringBuilder(); 
      String line; 
      while ((line = r.readLine()) != null) { 
       total.append(line); 
      } 
      instream.close(); 
      String result = total.toString(); 
      TLLog.d(TAG,"Image posting result : "+ result); 
      return true; 
     } 
    } 
} catch (ClientProtocolException e) { 
    TLLog.e(TAG, e.getStackTrace().toString()); 
} catch (IOException e) { 
    TLLog.e(TAG, e.getStackTrace().toString()); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

我無法理解的問題。

+0

如果問題出在圖像上,我猜'fileStream'在把它放入JSON對象時已經是錯誤的了。 – Henry 2013-03-02 10:18:10

+0

感謝您的回覆。現在我正在努力。 – Sanath 2013-03-02 11:21:33

回答

0

嗨,大家好,我終於找到了問題。 問題是編碼,所以我使用我從互聯網上下載的類Base64.java。我使用以下方法進行編碼:

public static String encodeTobase64(Bitmap image) throws IOException 
{ 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    image.compress(Bitmap.CompressFormat.JPEG, 50, outputStream); 
    byte[] bs = outputStream.toByteArray(); 
    String imageEncoded=Base64.encodeBytes(bs); 
    return imageEncoded; 
} 

並將返回值用作文件流。所以我的問題解決了。

相關問題