我试图使用我在c#中创建的这个网络服务:
[WebMethod]
public Student StudentDetails(string sName)
{
Student objStd = new Student();
SqlConnection conn;
conn = Class1.ConnectionManager.GetConnection();
conn.Open();
SqlCommand newCmd = conn.CreateCommand();
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "select * from dbo.tblUser where name='" + sName + "'";
SqlDataReader sdr = newCmd.ExecuteReader();
if (sdr.Read())
{
objStd.id = Int32.Parse(sdr["Id"].ToString());
objStd.name = sdr["name"].ToString();
objStd.grade = sdr["grade"].ToString();
}
conn.Close();
sdr.Close();
return objStd;
}
public class Student
{
public int id;
public string name;
public string grade;
}
取id并返回带有值的对象。
我试图在Android应用程序中调用它,如下所示:
mainactivty.java:
public class MainActivity extends Activity {
boolean timeoutexcep=false,httpexcep=false,generalexcep=false;
String nam="lama";
String Id,name,grade;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Persondetails().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
class Persondetails extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
this.dialog.setMessage("Loading data");
this.dialog.show();
}
@Override
protected Void doInBackground(Void... unused) {
final String NAMESPACE = "http://tempuri.org/";
final String URL = "http://192.168.1.5:90/myWebService.asmx";
final String SOAP_ACTION = "http://tempuri.org/StudentDetails";
final String METHOD_NAME = "StudentDetails";
SoapObject request2 = new SoapObject(NAMESPACE, METHOD_NAME);
request2.addProperty("sName", nam);
Bean C = new Bean();
PropertyInfo pi = new PropertyInfo();
pi.setName("Bean");
pi.setValue(C);
pi.setType(C.getClass());
request2.addProperty(pi);
SoapSerializationEnvelope envelope2 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope2.dotNet = true;
envelope2.setOutputSoapObject(request2);
envelope2.addMapping(NAMESPACE, "Bean", new Bean().getClass());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope2);
SoapObject response2 = (SoapObject) envelope2.getResponse();
System.out.println("check Request" + request2);
System.out.println("check response" + response2);
envelope2.addMapping(NAMESPACE, "Panchayat",new Bean().getClass());
androidHttpTransport.call(SOAP_ACTION, envelope2);
Bean[] personobj = new Bean[response2.getPropertyCount()];
Bean beanobj = new Bean();
for (int j = 0; j < personobj.length; j++) {
SoapObject pii = (SoapObject) response2.getProperty(j);
beanobj.Id = Integer.parseInt(pii.getProperty(1).toString());
beanobj.name = pii.getProperty(2).toString();
beanobj.grade = pii.getProperty(3).toString();
personobj[j] = beanobj;
}
name=beanobj.name;
Id=String.valueOf(beanobj.Id);
grade=beanobj.grade;
}
catch(SocketTimeoutException e){
timeoutexcep=true;
e.printStackTrace();
}
catch(ConnectException e){
httpexcep=true;
e.printStackTrace();
}
catch (Exception e) {
generalexcep=true;
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
if(timeoutexcep){
Toast.makeText(MainActivity.this, "Unable to connect to server, Please try again later",Toast.LENGTH_LONG).show();
}
else if(httpexcep){
Toast.makeText(MainActivity.this, "Please check your Internet connection",Toast.LENGTH_LONG).show();
}
else if(generalexcep){
Toast.makeText(MainActivity.this, "Please try later",Toast.LENGTH_LONG).show();
}
else {
display();
}
timeoutexcep=false;httpexcep=false;generalexcep=false;
}
private void display() {
TextView t1=(TextView)findViewById(R.id.name);
t1.setText(name);
TextView t2=(TextView)findViewById(R.id.palce);
t2.setText(grade);
TextView t3=(TextView)findViewById(R.id.age);
t3.setText(Id);
TextView t4=(TextView)findViewById(R.id.phone);
t4.setText("COOOL!");
}
}
}
Bean.java
public class Bean implements KvmSerializable
{
public int Id;
public String name;
public String grade;
public Bean(){}
public Bean( int Id,String name,String grade)
{
this.Id=Id;
this.name=name;
this.grade=grade;
}
public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return Id;
case 1:
return name;
case 2:
return grade;
}
return null;
}
public int getPropertyCount() {
return 4;
}
@SuppressWarnings("rawtypes")
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch(index)
{
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Id";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "name";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "grade";
break;
default:
break;
}
}
public void setProperty(int index, Object value) {
switch(index)
{
case 0:
Id = Integer.parseInt(value.toString());
break;
case 1:
name = value.toString();
break;
case 2:
grade = value.toString();
break;
default:
break;
}
}
}
但这总是会在以后再试一次吐司,我不知道我的代码有什么问题?有人可以帮忙吗?