从android添加多个参数到webservice

时间:2015-11-27 14:14:11

标签: java android web-services android-studio

我有这个网络服务:

   [WebMethod]
    public string AddStudent(string name)
    {

        return addSt(name);
    }

    public String addSt(String name)
    {

        SqlConnection conn;
        conn = Class1.ConnectionManager.GetConnection();
        conn.Open();

        SqlCommand newCmd = conn.CreateCommand();

        newCmd.CommandType = CommandType.Text;
        newCmd.CommandText = "insert into dbo.tblUser values('" + name + "','2')";
        SqlDataReader sdr = newCmd.ExecuteReader();

        conn.Close();
        return "successfully done";


    }

将新记录添加到我的数据库中,在表格中我有id(自动增量),名称和等级。

我有这个java可以调用webservice并从那里添加值:

   public class MainActivity extends AppCompatActivity {

private EditText editText;
private TextView textView;
private Handler mHandler= new Handler();



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText)findViewById(R.id.editText);
    textView = (TextView)findViewById(R.id.textView);
}
public void getName(View v){

    String inputId =editText.getText().toString();
    //String[] params= new String[]{"10.0.2.2",inputId};
    String[] params= new String[]{"192.168.1.17:90",inputId};
    new MyAsyncTask().execute(params);

}

class MyAsyncTask extends AsyncTask<String, Void, String>
{

    public String SOAP_ACTION="http://tempuri.org/AddStudent";
    public String OPERATION_NAME ="AddStudent";
    public String WSDL_TARGET_NAMESPACE ="http://tempuri.org/";
    public String SOAP_ADDRESS;
    private SoapObject request;
    private HttpTransportSE httpTransport;
    private SoapSerializationEnvelope envelop;
    Object response= null;


    @Override
    protected String doInBackground(String... params) {
        SOAP_ADDRESS="http://"+params[0]+"/myWebService.asmx";
        request= new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
        PropertyInfo pi=new PropertyInfo();
        pi.setName("name");
        pi.setValue(params[1]);
        pi.setType(Integer.class);
        request.addProperty(pi);
        pi= new PropertyInfo();

        envelop= new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelop.dotNet=true;
        envelop.setOutputSoapObject(request);
        httpTransport=new HttpTransportSE(SOAP_ADDRESS);
        try{
            httpTransport.call(SOAP_ACTION,envelop);
            response=envelop.getResponse();
        }
        catch (Exception e){
            response=e.getMessage();
        }
        return response.toString();
    }
    @Override
    protected void onPostExecute(final String result){
        super.onPostExecute(result);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                textView.setText(result);
            }
        });
    }
   }
}

这只是添加名称,我应该在这些值中更改:

   PropertyInfo pi=new PropertyInfo();
        pi.setName("name");
        pi.setValue(params[1]);
        pi.setType(Integer.class);
        request.addProperty(pi);
        pi= new PropertyInfo();

所以我可以从android添加名称和等级吗?

我尝试添加编辑文本并将其值设置为:

        pi.setName("name","grade");
        pi.setValue(params[1],params[2]);

但是我有错误,怎么做才能正确?

另请注意,我知道必须在网络服务中进行的更改,以便添加名称和成绩。

1 个答案:

答案 0 :(得分:0)

您可以为请求添加任意数量的属性

PropertyInfo pi=new PropertyInfo();
        pi.setName("name");
        pi.setValue(params[0]);// Generally array index starts from 0 not 1
        pi.setType(Integer.class);
        request.addProperty(pi);

pi= new PropertyInfo();

pi.setName("grade");
        pi.setValue(params[1]);
        pi.setType(Integer.class);
        request.addProperty(pi);