KSOAP Android和ASP.NET Web服务

时间:2014-08-14 01:51:54

标签: android asp.net web-services soap android-ksoap2

我想知道是否有人能够帮助我理解这个问题。我正在使用一个包含Web方法来存储数据的Web服务。使用kso​​ap库从Android手机发送数据以发送信封。我能够将数据从手机发送到我的服务器,并且web方法可以通过NPGSQL和POSTGRES执行该功能来存储数据。

当我发送数据超过20次时,问题就开始了,Web服务不再接收数据。我不知道这是ksoap的问题还是我的网络服务有问题。

Android代码

soap_server_retrieve.setOnClickListener(new OnClickListener() {
    volatile boolean flag = true;           

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

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            String result;
            String NAMESPACE = "http://demo.android.org/";
            String URL = "http://192.168.1.9/AndroidServer/WebService.asmx";
            String METHOD_NAME = "sensor_value";
            String SOAP_ACTION = "http://demo.android.org/sensor_value";
            int TimeOut=3000;

            @Override
            public void run() {
                try{
                    StrictMode.ThreadPolicy policy = new    StrictMode.ThreadPolicy.Builder().permitAll().build();
                    StrictMode.setThreadPolicy(policy);
                    SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
                    request.addProperty("a",  Double.parseDouble(textTEMPERATURE_reading.getText().toString()));

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    envelope.implicitTypes= true;
                    envelope.dotNet = true;
                    envelope.encodingStyle= SoapSerializationEnvelope.XSD;
                    envelope.setOutputSoapObject(request);
                    MarshalDouble md = new MarshalDouble();
                    md.register(envelope);

                    HttpTransportSE transport = new HttpTransportSE(URL,TimeOut);
                    try {
                        transport.call(SOAP_ACTION, envelope);
                        SoapPrimitive result_xml = (SoapPrimitive)     envelope.getResponse();
                        result = result_xml.toString();
                    } catch (HttpResponseException e) { 
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        //Toast.makeText(Ambiental_temperature.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        //Toast.makeText(Ambiental_temperature.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    } catch (XmlPullParserException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        //Toast.makeText(Ambiental_temperature.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                catch(Exception e)
                {
                    txt_server_value.setText(e.toString());
                }                       

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        Toast.makeText(Ambiental_temperature.this,
                                result, Toast.LENGTH_LONG).show();
                        txt_server_value = (TextView) findViewById(R.id.txt_server_value);
                        //TextView result = (TextView) findViewById(R.id.txt_server_value);
                        //txt_server_value.setText("Sensor.TYPE_AMBIENT_TEMPERATURE NOT Available");
                        txt_server_value.setText(result);
                    }
                });
            }
        }, 0, 5000);
    }
});

ASP.NET网络服务

[WebMethod(CacheDuration = 60)]
public double sensor_value(double a)
{
    DateTime now = DateTime.Now;
    string query;
    query = "insert into sensor_values VALUES (@id,@sensor_name,@value,@date)";
    NpgsqlConnection cnt = new NpgsqlConnection("Server=localhost;Port=5433; User Id=postgres;Password=admin;Database=Android_Sensors");
    cnt.Open();        
    NpgsqlCommand command = new NpgsqlCommand(query, cnt);
    command.Parameters.Add(new NpgsqlParameter("id", "1"));
    command.Parameters.Add(new NpgsqlParameter("sensor_name", "Ambiental Temperature"));
    command.Parameters.Add(new NpgsqlParameter("value", a));
    command.Parameters.Add(new NpgsqlParameter("date",now));
    try
    {
        NpgsqlDataReader dr = command.ExecuteReader();
    }                
    catch (NpgsqlException ex)
    {

    }
    cmb_Tables.Text + " and " + cmb_Tables2.Text, "");
    table in the datagridview dgv_table

    return a;
}

我不明白的是,为什么当我向我的网络服务发送20次数据时,它无法再接收?

如果重建网页并重新发布网站,我可以再次发送20个值,然后我再也无法通过手机发送了哪一种暗示可能是我的网络服务是问题。我真的很感激一些帮助。

1 个答案:

答案 0 :(得分:0)

如果有多个电话,请在每次通话后尝试断开连接。

transport.getConnection().disconnect();