2013-06-05 71 views
0

我是新的android编程(第一个项目),我需要帮助,使我的代码工作... 好吧,所以我有一个方法,将调用web服务,并得到从那里的字符串。它正在对AVD(上2.2)调用.net web服务与Android使用kso​​ap2

下面是代码:

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); 

虽然,它没有为我工作。 ..

回答

1

从android 3.0+你不能从主线程调用Web服务。将这整个代码写入AsyncClassDoInBackground

AsyncClass无法将值返回给调用类。 return这里传递的值为onPostExecute。使用folloing功能在您的AsyncClass

protected void onPostExecute(String result) 
{ 
//from here you have to pass your value to your main class 
} 
+0

谢谢你的答案,是的,我知道。但如果我这样做,那么代码不适合我(没有做任何事)... – 4448932

+0

你是如何做到这一点使用AsyncClass?发布代码。 –

+0

我编辑第一篇文章... – 4448932

0

或者你也可以加入这一行的活动,你只是在的setContentView(后)调用WebService的

if (android.os.Build.VERSION.SDK_INT > 9) { 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
    } 

,而且应该做的伎俩

相关问题