2013-06-05 95 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); 
    } 

,而且應該做的伎倆

相關問題