我是 android 编程的新手(第一个项目),我需要帮助来使我的代码正常工作......好的,所以我有一个方法可以调用 web 服务并从那里获取字符串。它正在处理 avd(在 2.2 上)
这是代码:
public void NewCall(int Position)
{
String SOAP_ACTION = "http://tempuri.org/";
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "";
String URL = "http://test.com/Service1.asmx?WSDL";
if(Position == 0)
{
SOAP_ACTION += "Method1";
METHOD_NAME = "Method1";
}
else if(Position == 1)
{
SOAP_ACTION += "Method2";
METHOD_NAME = "Method2";
}
else if(Position == 2)
{
SOAP_ACTION += "Method3";
METHOD_NAME = "Method3";
}
else if(Position == 3)
{
SOAP_ACTION += "Method4";
METHOD_NAME = "Method4";
}
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
String textRez = result.getProperty(0).toString();
addRow(textRez); //method for inserting new row in the database
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
但是正如我所读到的,您不能在 android 3.0+ 的“主线程”中执行此操作,作为解决方案,我看到您可以使用AsyncClass
. 关于这一点,我尝试将我找到的示例与我的代码结合起来,但它还没有给我结果......我认为我需要将位置作为整数解析为 AssyncClass,执行所需的代码doInBackground,然后我需要将结果作为字符串。我试图创作类似这样的东西,但它没有给我成功......
因此,如果你们中的某个人可以帮助/指导我解决这个问题,我将不胜感激。
谢谢 。
编辑:这就是我尝试使用 AsyncClass 的方式:
private class wsGet extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
int Position = Integer.parseInt(params[0]);
String SOAP_ACTION = "http://tempuri.org/";
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "";
String URL = "http://test.com/Service1.asmx?WSDL";
if(Position == 0)
{
SOAP_ACTION += "Method1";
METHOD_NAME = "Method1";
}
else if(Position == 1)
{
SOAP_ACTION += "Method2";
METHOD_NAME = "Method2";
}
else if(Position == 2)
{
SOAP_ACTION += "Method3";
METHOD_NAME = "Method3";
}
else if(Position == 3)
{
SOAP_ACTION += "Method4";
METHOD_NAME = "Method4";
}
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
tekstRez = result.getProperty(0).toString();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return tekstRez;
}
然后为了调用它,我尝试了:
public void NewTenders(int Pos)
{
String Position = String.valueOf(Pos);
String[] params= {Position}; //to pass to doInBackground
//Pass your args array and the current activity to the AsyncTask
String tekst = "";
try {
tekst = new wsGet().execute(params).get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}addRow(tekst);
虽然,它对我不起作用......