如何在android中的webservice中保存值?

时间:2012-01-11 09:19:32

标签: android .net web-services

我无法将值保存到.net中的网络服务器中。我使用以下代码,但我在emulator ..i.e ..,

中得到了错误
Server was unable to process request---> cannot insert the value null into column 'Name', table 'MyWorldApp.dbo.tbl_UserRegistration';column doesnot allow nulls.INSERT fails. The Statement has been terminated.

我使用了以下链接:http://113.193.181.53/MyWorldApp/Service1.asmx?op=InsertUsertRegistrationDetails

我的代码是:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.*;
    import android.os.*;
    import android.util.Log;
    import android.widget.TextView;

    public class MyworldActivity extends Activity {
        /** Called when the activity is first created. */

        private static final String SOAP_ACTION = "http://localhost/service1/InsertUsertRegistrationDetails";

        private static final String METHOD_NAME = "POST";

        private static final String NAMESPACE = "http://localhost/service1";
        private static final String URL = "http://113.193.181.53/MyWorldApp/Service1.asmx";



        TextView tv;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tv=(TextView)findViewById(R.id.text1);
            call();

        }

        public void call()
        {
            try {

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                request.addProperty("Name", "'Rajapandian'");
                request.addProperty("UserName", "Rajapandian");
                request.addProperty("Password", "123");
                request.addProperty("MobileNumber", "456");
                request.addProperty("EmailID", "Rajapandian@gmail.com");
                request.addProperty("image", "http://www.thehindu.com/multimedia/dynamic/00880/INDIA_CORRUPTION_PR_880168f.jpg");
                Log.e("success","success");
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet=true;
                envelope.setOutputSoapObject(request);

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

                Object result = (Object)envelope.getResponse();

                tv.setText(result.toString());
            } catch (Exception e) {
                tv.setText(e.getMessage());
                }
        }
    }

任何人都可以告诉我我做错了什么..

提前感谢..

3 个答案:

答案 0 :(得分:2)

而不是

Object result = (Object)envelope.getResponse();

试试这个

SoapObject response= (SoapObject) envelope.bodyIn;

答案 1 :(得分:0)

这是解决方案, 在这个例子中,我使用了get method.just在url字符串中传递你的变量

String url = "http://113.193.181.53/MyWorldApp/Service1.asmx/InsertUsertRegistrationDetails?Name=string&UserName=string&Password=string&MobileNumber=string&EmailID=string&image=string"

URL sourceUrl = new URL(url);
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            Handler1 dataHandler = new Handler1();
            xr.setContentHandler(dataHandler);
            xr.parse(new InputSource(sourceUrl.openStream()));
            Dataset dataset = dataHandler.getParsednewJobdtl_DataSet();

数据集:

package com.RecordingApp;

public class AuthDataset {



    private String booleanx = null;

    public void setbooleanx (String booleanx ) {
        this.booleanx = booleanx ;
    }

    public String getbooleanx () {
        return booleanx ;
    }


}

<强>处理程序:

package com.RecordingApp;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class AuthHandler extends DefaultHandler {
    boolean int1 = false;

    ArrayList ar1 = new ArrayList();

    ArrayList br1 = new ArrayList();

    private AuthDataset mysitelist = new AuthDataset();

    public AuthDataset getParsednewJobdtl_DataSet() {
        return this.mysitelist;
    }

    public void startDocument() throws SAXException {
        this.mysitelist = new AuthDataset();
    }

    @Override
    public void endDocument() throws SAXException {
    }

    // ***********************Start Element********************
    @Override
    public void startElement(String namespaceURI, String localName,
            String qName, Attributes atts) throws SAXException {
        if (localName.equals("boolean ")) {
            this.int1 = true;
        }

    }

    // ***********************End element********************
    @Override
    public void endElement(String namespaceURI, String localName, String qName)
            throws SAXException {
        if (localName.equals("boolean ")) {
            this.int1 = false;

        }
    }

    // ***********************character********************
    @Override
    public void characters(char ch[], int start, int length) {
        if (this.int1) {
            mysitelist.setbooleanx(new String(ch, start, length));
            ar1.add(new String(ch, start, length));
            setbooleanx (ar1);

        }

    }

    public void setbooleanx (ArrayList ar1) {
        this.br1 = ar1;
    }
    public ArrayList getbooleanx () {

        return br1;
    }

}

答案 2 :(得分:0)

  

服务器无法处理请求---&gt;无法插入值null   到列'名称',表'MyWorldApp.dbo.tbl_UserRegistration';列   不允许nulls.INSERT失败。该声明已被终止。

以上是你的错误。 您在字段名称中插入空值,并且名称设置为不接受空值

  • 因此修改数据库表
  • 或强制在名称字段中添加非空值

修改

将值发布到Web服务的方法是将空值发送到服务器