通过ksoap2发送数据

时间:2015-05-14 19:49:19

标签: java android ksoap2

我正在通过ksoap2创建一个外部数据库!我的Web服务工作正常,它在我的数据库中插入值,但通过Android它不发送任何值。我无法弄清楚这是什么错误!谁能帮帮我吗?我有一个Button和3 EditText用于向我的数据库发送(rollno name dues)

这是我的代码:

package com.example.externaldatabaseksoap2;
import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
    Button enter;
    EditText rollno,name,dues;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         final String NAMESPACE = "http://tempuri.org";
         final String METHOD_NAME = "insert";
         final String SOAP_ACTION = "http://tempuri.org/insert";
         final String URL = "http://192.168.1.6:5000/Service.asmx";

        rollno=(EditText)findViewById(R.id.etrollno);
        name=(EditText)findViewById(R.id.etname);
        dues=(EditText)findViewById(R.id.etdues);
        enter=(Button)findViewById(R.id.enter);
        enter.setOnClickListener(new View.OnClickListener() {

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




                Thread networkthread = new Thread(){

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        String r=rollno.getText().toString();
                        String n=name.getText().toString();
                        String d=dues.getText().toString();

                        try{
                            SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
                            PropertyInfo pi = new PropertyInfo();
                            pi.setName("rollno");
                            pi.setValue(r);
                            pi.setType(int.class);
                            request.addProperty(pi);

                            PropertyInfo pi1 = new PropertyInfo();
                            pi1.setName("name");
                            pi1.setValue(n);
                            pi1.setType(String.class);
                            request.addProperty(pi1);

                            PropertyInfo pi2 = new PropertyInfo();
                            pi2.setName("dues");
                            pi2.setValue(d);
                            pi2.setType(String.class);
                            request.addProperty(pi2);

                            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                            envelope.dotNet = true;
                            envelope.setOutputSoapObject(request);

                            HttpTransportSE ht = new HttpTransportSE(URL);
                            ht.call(SOAP_ACTION, envelope);

                            //runOnUiThread (new Runnable(){
                                //public void run(){
                                    //Toast.makeText(MainActivity.this,"Inserted", Toast.LENGTH_SHORT).show();
                                }
                            //});
                        //}
                        catch (Exception e){
                            e.printStackTrace();
                            //Toast.makeText(MainActivity.this,e.getMessage().toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                    };
                        networkthread.start();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

我认为您应该使用AsyncTask代替Thread,例如:

private class ExampleTask extends AsyncTask<URL, Integer, Long> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();       
    }

    protected Long doInBackground(URL... urls) {

        return 0;       
    }

    protected void onPostExecute(Long result) {
        super.onPostExecute(result);

    }

}

AsyncTask<URL, Integer, Long>的含义是AsyncTask<Params, Progress, Result>。所以你可以调整你的代码。

有关详细信息,请查看以下网站:http://karanbalkar.com/2014/03/tutorial-78-using-ksoap2-in-android/http://developer.android.com/reference/android/os/AsyncTask.html