Java 将多个参数从android添加到webservice
我有一个Web服务:Java 将多个参数从android添加到webservice,java,android,web-services,android-studio,Java,Android,Web Services,Android Studio,我有一个Web服务: [WebMethod] public string AddStudent(string name) { return addSt(name); } public String addSt(String name) { SqlConnection conn; conn = Class1.ConnectionManager.GetConnection(); con
[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);
}
});
}
}
}
那么我可以从android添加名称和等级
我尝试添加编辑文本,并将其值如下所示:
pi.setName("name","grade");
pi.setValue(params[1],params[2]);
但是我有错误,如何正确地做呢
还请注意,我知道必须在webservice中进行的更改,以便它可以添加名称和等级。您可以在请求中添加任意数量的属性
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);
非常感谢!!工作得很好。
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);