Android Socket被关闭错误

时间:2015-06-15 08:39:16

标签: java android sockets

我正在开发一个Android应用程序,我正在尝试连接服务器以进行用户名验证。我收到错误" java.net.socketException:Socket已关闭" ......如果有人可以提供帮助,我会在下面给出我的代码......

MainActivity

webserviceCallButton   = (Button)findViewById(R.id.login_Button);

        etUserName = (EditText)findViewById(R.id.editTextLogin);
        etPassword = (EditText)findViewById(R.id.editTextPassword);
        webserviceResponse = (TextView) findViewById(R.id.textView1);

        webserviceCallButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                 webserviceResponse.setText("Requesting to server .....");

                    //Create Webservice class object
                    WebserviceCall com = new WebserviceCall(); 

                    // Initialize variables
                    String id  = "99999999999";
                    String password = "01233333";


                    //Call Webservice class method and pass values and get response
                    String aResponse = com.getmLogin("mLo",id,password);   

                    //Alert message to show webservice response
                    Toast.makeText(getApplicationContext(), id+" id= "+aResponse+" hi", 
                       Toast.LENGTH_LONG).show();

                    Log.i("AndroidExampleOutput", "----"+aResponse);

                    webserviceResponse.setText("Response : "+aResponse);



            }
        });
    }

WebserviceCall

String namespace   = "https://xxxx.xxxx.com/";
    private String url = "https://xxxx.xxxx.xxxxxx.com/appservice/d_service.asmx";

    String SOAP_ACTION = "https://xxxxx.com/mLo";
    SoapObject request = null, objMessages = null;
    SoapSerializationEnvelope envelope;
    AndroidHttpTransport androidHttpTransport;

    WebserviceCall() {
    }


    /**
     * Set Envelope
     */
    protected void SetEnvelope() {

        try {

            // Creating SOAP envelope           
            envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            //You can comment that line if your web service is not .NET one.
            envelope.dotNet = true;

            envelope.setOutputSoapObject(request);
            androidHttpTransport = new AndroidHttpTransport(url);
            androidHttpTransport.debug = true;

        } catch (Exception e) {
            System.out.println("Soap Exception---->>>" + e.toString());    
        }
    }

    // MethodName variable is define for which webservice function  will call
    public String getmLogin(String mLo, String customerid,
            String customername) 
      {

        try {

            //SOAP_ACTION = namespace + MethodName;
            //SOAP_ACTION = namespace + MethodName;

            //Adding values to request object
            request = new SoapObject(namespace, mLo);


            //Adding String value to request object
            request.addProperty("customerid", "" + customerid);
            request.addProperty("customername", "" + customername);

            SetEnvelope();

            try {

                //SOAP calling webservice
                androidHttpTransport.call(SOAP_ACTION, envelope);

                //Got Webservice response
                String result = envelope.getResponse().toString();

                return result;

            } catch (Exception e) {
                // TODO: handle exception
                return e.toString();
            }
        } catch (Exception e) {
            // TODO: handle exception
            return e.toString();
        }

    }

1 个答案:

答案 0 :(得分:2)

此异常表示您已关闭套接字并尝试再次使用它。

如果关闭输入或输出流,它将自动关闭另一个流和Socket。

完成完成后关闭