简体   繁体   中英

KSOAP Android and ASP.NET web service

I would like to know if somebody is able to help me out to understand this problem. I'm using a web service which contains a web method to store data. The data is sent from an Android phone using ksoap library to send the envelopes. I'm able to send data from the phone to my server, and the web method works executing the function with NPGSQL and POSTGRES to store the data.

The problem starts when I send data more than 20 times, the web service does not receive the data anymore. I don't know whether this is a problem of ksoap or a problem with my web service.

Android code

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 web service

[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;
}

What I don't understand is, why exactly when I send 20 times data to my web service, it is not able to receive anymore?

If rebuild the webpage and I republish the website, I'm able to send again 20 values, and then I can't send anymore from the phone which kind of gives me a hint that maybe my web service is the problem. I would really appreciate some help.

如果有多个呼叫,请在每次呼叫后尝试断开连接。

transport.getConnection().disconnect();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM