【问题标题】:Sending byte array with ksoap2 on android在android上使用kso​​ap2发送字节数组
【发布时间】:2013-02-01 16:33:28
【问题描述】:

我正在尝试将图像从 android 设备上传到 WCF 服务。如果我发送一个小图像(大约 20kb),那么它工作正常。如果我发送稍微大一点的图片(大约 95kb),我会收到一个错误:

org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:1 in java.io.InputStreamReader@41636470)

Android 代码是:

    byte[] fileContents = IOUtil.readFile(image.getFileName());
    request = new SoapObject(CommonFunctions.NAMESPACE, "SaveAttachment");
    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    new MarshalBase64().register(envelope);
    androidHttpTransport = new HttpTransportSE(CommonFunctions.SERVICE_URL);

    request.addProperty("SomeProperty1", somevalue1);
    request.addProperty("SomeProperty2", somevalue2);
    PropertyInfo p1=new PropertyInfo();
    p1.setName("FileContents");
    p1.setType(MarshalBase64.BYTE_ARRAY_CLASS);
    p1.setValue(fileContents);
    request.addProperty(p1);
    androidHttpTransport.call(CommonFunctions.SOAP_ACTION + "SaveAttachment", envelope);
    int fileId = Integer.parseInt(envelope.getResponse().toString());

几秒钟后在 androidHttpTransport.call 行上抛出异常。我的 WCF 服务中的断点永远不会被命中。我提高了 WCF 绑定的请求限制:

  <basicHttpBinding>
    <binding name="MobileIntegrationBinding" messageEncoding="Text" allowCookies="true" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
      <security mode="Transport" />
    </binding>
  </basicHttpBinding>

KSOAP 将添加到属性的数据量是否有一些限制,如果有,是否有办法增加?

【问题讨论】:

  • 不太了解 WCF 或 .Net 我只能假设在服务器上的断点之前客户端和服务器之间可能会发生一些额外的握手。您的 XmlPullParserException 证明客户端正在尝试解析一些 XML(可能来自服务器?)并过早地到达响应的末尾。也许发布客户端异常的堆栈跟踪?

标签: android ksoap2


【解决方案1】:

啊,终于明白了。我的绑定和服务声明在 web.config 中都搞砸了,所以我对 maxReceivedMessageSize 等的更改没有任何效果,因为服务使用的是默认绑定而不是使用我的自定义绑定。

【讨论】:

    【解决方案2】:

    你需要压缩图像

    String imageEncoded;
    InputStream is = activity.getContentResolver().openInputStream(uri);
    Bitmap img = BitmapFactory.decodeStream(is);
    ByteArrayOutputStream convert = new ByteArrayOutputStream();
    img.compress(Bitmap.CompressFormat.JPEG, 50, convert);
    byte[] b = convert.toByteArray();
    imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    

    然后使用Base64无字节转换.. 在 android 上只能转换为 50mb 左右的字节数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多