I am fairly new to programming and just started out with WebService using ASP.NET C# in Microsoft Visual Studio 2017. I also downloaded KSoap2 into Android Studio and plan to insert user data into SQL Server DB via the WebService. However, nothing happened. It would be great if you all could take a look at my code and give me feedback as to what is wrong. Thank you all!!
我对编程很新,刚开始使用Microsoft Visual Studio 2017中的ASP.NET C#。我还将KSoap2下载到Android Studio中,并计划通过WebService将用户数据插入SQL Server DB。然而,什么也没发生。如果你们都能看看我的代码并给出反馈,那就太好了。谢谢你们!!
Here is my code for ASP.NET WebService:
这是我的ASP.NET WebService代码:
[WebMethod]
public Boolean InsertUser(string firstName, string lastName, string email, string password)
{
SqlConnection conn = ConnectionManager.GetConnection();
conn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO dbo.UserData(firstName, lastName, email, password) VALUES('" + firstName + "', '" + lastName + "', '" + email + "', '" + password + "')", conn);
cmd.ExecuteNonQuery();
conn.Close();
return true;
}
And here is my code in Android Studio:
这是Android Studio中的代码:
public class TestCreateAcc extends AppCompatActivity {
TextView tvS;
EditText etFN, etLN, etE, etP, etCP;
Button btnSub;
String firstName, lastName, email, password, confirmPwd;
String displayText;
private static final String SOAP_ACTION = "http://tempuri.org/InsertUser";
private static final String OPERATION_NAME = "InsertUser";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://10.0.1.86:53877/Service.asmx";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_create_acc);
tvS = (TextView) findViewById(R.id.tvS);
btnSub = (Button) findViewById(R.id.btnSub);
btnSub.setOnClickListener( new OnClickListener(){
public void onClick(View v)
{
etFN = (EditText) findViewById(R.id.etFN);
etLN = (EditText) findViewById(R.id.etLN);
etE = (EditText) findViewById(R.id.etE);
etP = (EditText) findViewById(R.id.etP);
etCP = (EditText) findViewById(R.id.etCP);
firstName = etFN.getText().toString().trim();
lastName = etLN.getText().toString().trim();
email = etE.getText().toString().trim();
password = etP.getText().toString().trim();
confirmPwd = etCP.getText().toString().trim();
if(firstName.isEmpty() && lastName.isEmpty() && email.isEmpty() && password.isEmpty() && confirmPwd.isEmpty())
{
tvS.setVisibility(View.VISIBLE);
tvS.setText("Fields cannot be left empty!!!");
tvS.setTextColor(Color.parseColor("#FF0000"));
}
else if(!confirmPwd.equals(password))
{
tvS.setVisibility(View.VISIBLE);
tvS.setText("Passwords do not match!!!");
tvS.setTextColor(Color.parseColor("#FF0000"));
}
else
{
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
}
});
}
class AsyncCallWS extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute(){
tvS.setVisibility(View.INVISIBLE);
}
@Override
protected String doInBackground(String... params){
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
PropertyInfo FirstName = new PropertyInfo();
FirstName.setName("firstName");
FirstName.setValue(params[0]);
FirstName.setType(String.class);
PropertyInfo LastName = new PropertyInfo();
LastName.setName("lastName");
LastName.setValue(params[1]);
LastName.setType(String.class);
PropertyInfo Email = new PropertyInfo();
Email.setName("email");
Email.setValue(params[2]);
Email.setType(String.class);
PropertyInfo Password = new PropertyInfo();
Password.setName("password");
Password.setValue(params[3]);
Password.setType(String.class);
request.addProperty(FirstName);
request.addProperty(LastName);
request.addProperty(Email);
request.addProperty(Password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
httpTransport.debug = true;
try{
httpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String res = response.toString() + "\n";
res += httpTransport.requestDump + "\n" + httpTransport.responseDump;
return res;
}catch(Exception ex){
return ex.toString();
}
}
@Override
protected void onProgressUpdate(String... values){
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
tvS.setVisibility(View.VISIBLE);
displayText = "Success!!";
tvS.setTextColor(Color.parseColor("#00FF00"));
tvS.setText(displayText);
Intent register = new Intent(TestCreateAcc.this, DriverLicenseActivity.class);
startActivity(register);
}
}
Thank you!! :)
谢谢!! :)
EDIT: I am using and actual Android Device and these are the errors I faced:
编辑:我正在使用和实际的Android设备,这些是我面临的错误:
org.apache.http.conn.HttpHostConnectException: Connection to http://mobilesystemservice.com refused returning data cannot be null! java.lang.ArrayIndexOutOfBoundsException: length=0; index=1
org.apache.http.conn.HttpHostConnectException:连接到http://mobilesystemservice.com拒绝返回数据不能为null! java.lang.ArrayIndexOutOfBoundsException:length = 0;索引= 1
1 个解决方案
#1
0
I am not sure if you are getting any values from the param
parameters. But what you can do is check first put all your code inside a try-catch
block, then check if you got any values from param
, then just set the parameter as a simple string.
我不确定你是否从param参数中获取任何值。但你可以做的是先检查所有代码是否在try-catch块中,然后检查你是否从param中获取了任何值,然后将参数设置为一个简单的字符串。
protected String doInBackground(String... params)
{
try
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
//add properties to SOAP object
//http://kobjects.org/ksoap2/doc/api/org/ksoap2/serialization/SoapObject.html#addProperty(java.lang.String, java.lang.Object)
request.addProperty("firstname", "");
request.addProperty("lastName", "");
request.addProperty("email", "");
request.addProperty("password", "");
int increment = 0;
//use foreach to avoid out of bound index
foreach(String p : params)
{
if (p != null)
{
//set properties to soap object
//http://kobjects.org/ksoap2/doc/api/org/ksoap2/serialization/KvmSerializable.html#setProperty(int, java.lang.Object)
switch(increment)
{
case 0:
request.setProperty(increment, p);
break;
case 1:
request.setProperty(increment, p);
break;
case 2:
request.setProperty(increment, p);
break;
case 3:
request.setProperty(increment, p);
break;
}
}
increment++;
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
httpTransport.debug = true;
httpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String res = response.toString() + "\n";
res += httpTransport.requestDump + "\n" + httpTransport.responseDump;
return res;
}
catch(Exception ex)
{
return ex.toString();
}
}
Just based it in this document
仅以此文档为基础
Hope this will help you.
希望这会帮助你。
`
`
#1
0
I am not sure if you are getting any values from the param
parameters. But what you can do is check first put all your code inside a try-catch
block, then check if you got any values from param
, then just set the parameter as a simple string.
我不确定你是否从param参数中获取任何值。但你可以做的是先检查所有代码是否在try-catch块中,然后检查你是否从param中获取了任何值,然后将参数设置为一个简单的字符串。
protected String doInBackground(String... params)
{
try
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
//add properties to SOAP object
//http://kobjects.org/ksoap2/doc/api/org/ksoap2/serialization/SoapObject.html#addProperty(java.lang.String, java.lang.Object)
request.addProperty("firstname", "");
request.addProperty("lastName", "");
request.addProperty("email", "");
request.addProperty("password", "");
int increment = 0;
//use foreach to avoid out of bound index
foreach(String p : params)
{
if (p != null)
{
//set properties to soap object
//http://kobjects.org/ksoap2/doc/api/org/ksoap2/serialization/KvmSerializable.html#setProperty(int, java.lang.Object)
switch(increment)
{
case 0:
request.setProperty(increment, p);
break;
case 1:
request.setProperty(increment, p);
break;
case 2:
request.setProperty(increment, p);
break;
case 3:
request.setProperty(increment, p);
break;
}
}
increment++;
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
httpTransport.debug = true;
httpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String res = response.toString() + "\n";
res += httpTransport.requestDump + "\n" + httpTransport.responseDump;
return res;
}
catch(Exception ex)
{
return ex.toString();
}
}
Just based it in this document
仅以此文档为基础
Hope this will help you.
希望这会帮助你。
`
`