2013-10-09 55 views
1

我使用Webview向交換郵件服務器發送數據。 (http post不適用於附件較大的郵件,因此嘗試使用此方法)。android:將數據發佈到webview

請參閱下面的代碼。

如果我發送的數據根本沒有編碼,發送失敗。 如果我按照我的代碼進行編碼整個數據,它仍然失敗。

如果我嘗試評論的代碼,我將數據存儲爲名稱valuepairs並進行編碼,則收到郵件但沒有附件。那麼在這裏編碼的正確方法是什麼?附件的類型是ContentBody。所有其他參數都是字符串。

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,); 
      entity.addPart("hidid", new StringBody(hidid)); 
      entity.addPart("hidchk", new StringBody(hidchk)); 
      entity.addPart("hidcanary", new StringBody(canary)); 
      entity.addPart("attach", attachment); 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      entity.writeTo(bytes); 
      String fullUrl = baseUrl + "?ae=Dialog&t=Attach&a=Add"; 
     webView.postUrl(fullUrl, EncodingUtils.getBytes(bytes.toString(),"BASE64")); 


      /* List<NameValuePair> parameters = new ArrayList<NameValuePair>(); 
      parameters.add(new BasicNameValuePair("hidid", hidid)); 
      parameters.add(new BasicNameValuePair("hidchk", hidchk)); 
      parameters.add(new BasicNameValuePair("hidcanary", canary)); 
      parameters.add(new BasicNameValuePair("attach", attachment.toString())); 
      UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(parameters); 
String fullUrl = baseUrl + "?ae=Dialog&t=Attach&a=Add"; 
    webView.postUrl(fullUrl, EntityUtils.toByteArray(entity1)); */ 

我看到他的Android web視圖:: postUrl方法更難編碼的「application/X WWW的窗體-urlencoded」。

+0

你在logcat中得到任何失敗消息嗎? –

+0

是的,我得到的錯誤響應是:Outlook Web訪問無法處理請求 – png

+0

嘗試將您的附件轉換爲base64中的註釋代碼 –

回答

0
bytes.toString() 

從您的字節創建一個字符串。使用toBytesArray()來代替,以獲得一個字節[]

然後,EncodingUtils使用Charset進行編碼。 BASE64不是一個字符集。 UTF-8是。如果你想爲Base64編碼您的字節,使用android.util.Base64.encode(byte[]);

在您的例子:

webView.postUrl(fullUrl, Base64.encode(bytes.toByteArray(), Base64.DEFAULT)); 
+0

謝謝。我試了一下,但再次努力工作 – png

+0

這可能不是您的服務器預期數據的方式。 – njzk2

0

每一個blog post about using MultipartEntity to post data to a url,你可能需要在項目中包含一些額外的jar文件。它們包括以下apache開源項目:apache-mime4j,httpclient,httpcore和httpmime。

一旦你這樣做,你應該能夠使用下面的代碼爲例&文件後兩個字符串的URL:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.tumblr.com/api/write"); 

try { 
    MultipartEntity entity = new MultipartEntity(); 

    entity.addPart("type", new StringBody("photo")); 
    entity.addPart("data", new FileBody(image)); 
    httppost.setEntity(entity); 
    HttpResponse response = httpclient.execute(httppost); 
} catch (ClientProtocolException e) { 
} catch (IOException e) { 
} 
+0

我擁有所有這些,但當我發送像這樣的服務器時會受到大小限制。它響應錯誤 – png

+0

如果服務器有一個大小限制,你嘗試通過webView是沒有任何區別!你有權訪問服務器? – SBerg413

1

嘗試轉換您的附件作爲

public static String encodeToBase64(String string) 
    { 
     String encodedString = ""; 
     try 
     { 
      byte[] byteData = null; 
      if(Build.VERSION.SDK_INT >= 8) // Build.VERSION_CODES.FROYO --> 8 
      { 
       byteData = android.util.Base64.encode(string.getBytes(),android.util.Base64.DEFAULT); 
      } 
      else 
      { 
       byteData = Base64Utility.encode(string.getBytes(),Base64Utility.DEFAULT); 
      } 
      encodedString = new String(byteData); 
     } 
     catch (Exception e) 
     { 
     } 
     return encodedString; 
    } 

以base64

+0

我編輯我的代碼來做到這一點。但是,郵件收到沒有附件。 – png

+0

parameters.add(new BasicNameValuePair(「hidcanary」,canary)); // parameters.add(new BasicNameValuePair(「attach」,attachment.toString())); parameters.add(new BasicNameValuePair(「attach」,encodeToBase64(attachment.toString()))); UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(parameters); String fullUrl = baseUrl +「?ae = Dialog&t = Attach&a = Add」; \t \t \t webView.postUrl(fullUrl,EntityUtils。toByteArray(使用實體)); – png

+0

UrlEncodedFormEntity已經做了我猜的編碼,所以doeas再次編碼附件有什麼區別? – png