我是android编程的新手(第一个项目),我需要帮助才能让我的代码工作...... 好的,所以我有一个方法可以调用webservice并从那里获取字符串。它正在开发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
做到这一点。关于这一点,我试图结合我发现的例子和我的代码,但它还没有给我结果......
我认为我需要将Position作为整数解析为AssyncClass,执行doInBackground中所需的代码,然后我需要将结果作为String。
我试着写出类似这样的东西,但它没有给我带来任何成功......
所以,如果你们中的某些人可以帮助/指导我解决这个问题,我将非常感激。
谢谢。
编辑:这是我尝试使用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);
虽然,它对我不起作用......
答案 0 :(得分:1)
从android 3.0+开始,您无法从主线程调用Web服务。将此整个代码写入DoInBackground
的{{1}}。
AsyncClass无法将值返回给调用类。 AsyncClass
此处将值传递给return
。使用AsyncClass中的folloing函数
onPostExecute
答案 1 :(得分:0)
或者您可以将此行添加到您正在调用webservice
的活动中 if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
就在setContentView()之后,那应该是诀窍