java.net.SocketException:sendto failed:发送byte []时EPIPE(Broken pipe)

时间:2014-01-20 07:46:52

标签: android web-services socketexception

我正在尝试上传通过Android手机录制的视频。但是当将视频作为byte []发送到服务器时,我收到以下错误:

  01-20 13:03:18.250: E/SaveMedia-response(4393): java.net.SocketException: sendto failed: EPIPE (Broken pipe)

以下是我尝试上传视频的方式:

        SoapObject request = new SoapObject(
                HelpMeConstant.WSDL_TARGET_NAMESPACE, "SaveMedia");
        Log.i("SaveMedia-parameter", "Email : " + Email + ", Media : " + Media
                + "MediaType : " + MediaType + ",MediaExt : " + MediaExt);

        MediaAvidance mediainfo = new MediaAvidance();
        mediainfo.EmailId = Email;
        mediainfo.Media = Media;
        mediainfo.MediaExt = MediaExt;
        mediainfo.MediaType = MediaType;

        PropertyInfo pi = new PropertyInfo();
        pi.setName("mediainfo");
        pi.setValue(mediainfo);
        pi.setType(mediainfo.getClass());

        request.addProperty(pi);
/*      request.addProperty("EmailId",Email);
         request.addProperty("Media", Media); 
         request.addProperty("MediaType", MediaType);
         request.addProperty("MediaExtn", MediaExt);
*/       
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        Log.i("SaveMedia","Serialising..");
        new MarshalBase64().register(envelope);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(
                HelpMeConstant.SOAP_ADDRESS);
        Object response = null;

        try {
            Log.i("SaveMedia","Calling service..");
            httpTransport.call(HelpMeConstant.SOAP_ACTION + "SaveMedia",
                    envelope);
            Log.i("SaveMedia","getting response..");
            response = envelope.getResponse();
        } catch (Exception exception) {
            response = exception.toString();
        }
        Log.i("response", response.toString());
        return response.toString();

它正在发送到.net网络服务,如下所示:

 [WebMethod]
    public string SaveMedia(MediaAvidance avidanceinfo)
    {
        rs = new ResultSet();
        rs = logictask.SaveMedia(avidanceinfo.EmailId,avidanceinfo.Media,avidanceinfo.MediaType,avidanceinfo.MediaExt);
        return rs.isSuccessfull;
    }

MediaAvidance:

public class MediaAvidance implements KvmSerializable {
    public String EmailId;
    public byte[] Media;
    public String MediaType;
    public String MediaExt;

    public MediaAvidance() {
    }

    public MediaAvidance(String EmailID, byte[] Media, String MediaType,
            String MediaExt) {

        this.EmailId = EmailID;
        this.Media = Media;
        this.MediaType = MediaType;
        this.MediaExt = MediaExt;
    }

    @Override
    public Object getProperty(int arg0) {
        switch (arg0) {
        case GETEMAILID:
            return EmailId;
        case GETMEDIA:
            return Media;
        case GETMEDIATYPE:
            return MediaType;
        case GETMEDIAEXT:
            return MediaExt;
        default:
            return null;
        }
    }

    @Override
    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 4;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch (index) {
        case 0:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "EmailId";
            break;
        case 1:
            info.type = PropertyInfo.OBJECT_CLASS;
            info.name = "Media";
            break;
        case 2:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "MediaType";
            break;
        case 3:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "MediaExt";
            break;
        default:
            break;
        }

    }

    @Override
    public void setProperty(int index, Object value) {
        switch (index) {
        case 0:
            EmailId = value.toString();
            break;
        case 1:
            Media = (byte[]) value;
            break;
        case 2:
            MediaType = value.toString();
            break;
        case 3:
            MediaExt = value.toString();
            break;
        default:
            break;
        }
    }
}

在服务器端,MediaAvidance类不要实现KvmSerializable及其方法。

我搜索了几天,在stackoverflow&中经历了很多问题。我知道我正在尝试在连接关闭时写入,并且它与套接字编程有关。 BUT

我不知道如何保持连接活着(我认为这可能是一个解决方案)。 请帮我。

谢谢, Sourabh

1 个答案:

答案 0 :(得分:0)

我终于管理好让它发挥作用。发布这个解决方案,希望它对像我这样的人有用。您可能在代码中遇到更多或任何以下问题,但我很乐意犯这3个错误,并从中吸取教训,这会导致视频上传出现问题:

  1. 我已经通过实施MediaAvidance在android中序列化了KvmSerializable,但为了使其工作,您的服务器端MediaAvidance也必须被序列化。我通过下面的代码完成了解决我的Cannot Serialize异常的问题:
  2.    [Serializable]
       public class MediaAvidance

    您还需要将SoapSerializationEnvelope注册到MarshalBase64

       envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
       new MarshalBase64().register(envelope); 
    1. 当你从android调用时,在PropertyInfo中设置与服务方法参数(例如:avidanceinfo)完全相同的名称,这个解决了faultstring: 'Server was unable to read request.异常:

      在android中定义的属性名称  

      pi.setName("avidanceinfo");

      服务方式:  

       [WebMethod]
       public string SaveMedia(MediaAvidance avidanceinfo)

    2. 最后你需要添加 envelope.implicitTypes = true ;
      以便HttpTransportSE能够在服务器上识别MediaAvidance类型,否则您将获得Object reference not set to an instance of an object. when using xs:anyType....例外。

    3. * 希望这可能对您有所帮助。 :) *