0

我正在尝试连接我的数据库,我需要一些选择,插入 vb。命令。我在互联网上有一个独立运行或与其他 .net 应用程序一起运行的 Web 服务。我为 android 编写了一些代码,但我无法正确运行。

public class MainActivity extends Activity {
    final static String NAMESPACE = "http://tempuri.org/";
    final static String METHOD_NAME = "MQ_Insert";
    final static String SOAP_ACTION = "http://tempuri.org/MQ_Insert";
    final static String URL = "http://mysite/Service1.asmx";

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

        final EditText txt1 = (EditText) findViewById(R.id.editText1);
        final EditText txt2 = (EditText) findViewById(R.id.editText2);
        final EditText txt3 = (EditText) findViewById(R.id.editText3);
        final EditText txt4 = (EditText) findViewById(R.id.editText4);
        final TextView txt=(TextView) findViewById(R.id.textView1);

        Button btn=(Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                SoapObject Request = new SoapObject(NAMESPACE,METHOD_NAME);
                Request.addProperty("QA", txt1.getText().toString());
                Request.addProperty("H_ID", Integer.parseInt(txt2.getText().toString()));
                Request.addProperty("MC_ID", Integer.parseInt(txt3.getText().toString()));
                Request.addProperty("CC_ID", Integer.parseInt(txt4.getText().toString()));

                SoapSerializationEnvelope soapEnvelop;
                soapEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                soapEnvelop.dotNet=true;
                soapEnvelop.setOutputSoapObject(Request);

                HttpTransportSE htp=new HttpTransportSE(URL);
                try {
                    htp.call(SOAP_ACTION, soapEnvelop);
                    //SoapPrimitive resultString = (SoapPrimitive)soapEnvelop.getResponse();
                    //String id = resultString.toString();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String result;
                try {
                    result= "final:"+(SoapPrimitive) soapEnvelop.getResponse();
                    txt.setText(result);
                } catch (SoapFault e) {
                    e.printStackTrace();
                }                   
            }
        });
    }

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

当我尝试使用 AVD 时,它被此代码停止,但在 eclipse 中似乎没问题。下面的 Web 服务代码,它工作正常。我找不到我的错误:(

 [WebMethod]
        public string MQ_Insert(string QA, int H_ID, int MC_ID, int CC_ID)
        {
            conn = new SqlConnection(constring);
            conn.Open();

            comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandType = System.Data.CommandType.StoredProcedure;
            comm.CommandText = "MQ_Insert";
            comm.Parameters.AddWithValue("@QA", QA);
            comm.Parameters.AddWithValue("@H_ID", H_ID);
            comm.Parameters.AddWithValue("@MC_ID", MC_ID);
            comm.Parameters.AddWithValue("@CC_ID", CC_ID);
            try
            {
                comm.ExecuteNonQuery();
                return "Record Saved";
            }
            catch (Exception)
            {
                return "Not Saved";
            }
            finally
            {
                conn.Close();
            }
        }

尝试此操作时出现错误:

在此处输入图像描述

4

0 回答 0