要如果曾与AsynxTasc工作,但我仍然不工作,使用教程安卓4.0.3? [英] To use the tutorial in android 4.0.3 if had to work with AsynxTasc but i still dont work?

查看:230
本文介绍了要如果曾与AsynxTasc工作,但我仍然不工作,使用教程安卓4.0.3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用本教程的http://www.c-sharpcorner.com/UploadFile/88b6e5/how-to-call-web-service-in-android-using-soap/并做了一些机会,它的工作768,16为Android 4.0.3! 但它仍然不是! 有人可以后我一个正确的解决方案?

 包com.webservice;
进口org.ksoap2.SoapEnvelope;
进口org.ksoap2.serialization.SoapObject;
进口org.ksoap2.serialization.SoapSerializationEnvelope;
进口org.ksoap2.transport.HttpTransportSE;
进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;


公共类FirstScreen扩展活动

{
/ **第一次创建活动时调用。 * /

私有静态字符串SOAP_ACTION1 =htt​​p://tempuri.org/FahrenheitToCelsius;
私有静态字符串SOAP_ACTION2 =htt​​p://tempuri.org/CelsiusToFahrenheit;
私有静态字符串NAMESPACE =htt​​p://tempuri.org/;
私有静态字符串METHOD_NAME1 =FahrenheitToCelsius;
私有静态字符串METHOD_NAME2 =CelsiusToFahrenheit;
私有静态字符串URL =htt​​p://www.w3schools.com/webservices/tempconvert.asmx?WSDL;
按钮btnFar,btnCel,btnClear;
的EditText txtFar,txtCel;

@覆盖
公共无效的onCreate(包savedInstanceState)

{
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_first_screen);
        btnFar =(按钮)findViewById(R.id.btnFar);
        btnCel =(按钮)findViewById(R.id.btnCel);
        btnClear =(按钮)findViewById(R.id.btnClear);
        txtFar =(EditText上)findViewById(R.id.txtFar);
        txtCel =(EditText上)findViewById(R.id.txtCel);

        btnFar.setOnClickListener(新View.OnClickListener()
        {
                 @覆盖
                  公共无效的onClick(视图v)
                  {
                      新one1()执行()。
                  }
            });
        btnCel.setOnClickListener(新View.OnClickListener()
        {
                  @覆盖
                  公共无效的onClick(视图v)
                  {
                      新one2()执行()。
                  }
            });

        btnClear.setOnClickListener(新View.OnClickListener()
        {
                  @覆盖
                  公共无效的onClick(视图v)
                  {
                        txtCel.setText();
                        txtFar.setText();
                  }
            });
 }

私有类one1扩展的AsyncTask<字符串,整数,字符串> {

    @覆盖
    保护字符串doInBackground(字符串...为arg0){
        // TODO自动生成方法存根
           //初始化SOAP请求+添加参数
        SoapObject请求=新SoapObject(命名空间METHOD_NAME1);

        //使用它来添加参数
        request.addProperty(华氏,txtFar.getText()的toString());

        //声明SOAP请求的版本
        SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(要求);
        envelope.dotNet = TRUE;

        尝试 {
              HttpTransportSE androidHttpTransport =新HttpTransportSE(URL);

              //这是将调用Web服务的实际组成部分
              androidHttpTransport.call(SOAP_ACTION1,包);

              //从信封体在SOA presult。
              SoapObject结果=(SoapObject)envelope.bodyIn;
              如果(结果!= NULL)
              {
                    //获取第一属性,并更改标签文本
                    txtCel.setText(result.getProperty(0)的ToString());
              }
              其他
              {
                    Toast.makeText(getApplicationContext(),不回应,Toast.LENGTH_LONG).show();
              }
        }赶上(例外五){

              e.printStackTrace();

        }
        返回null;
    }

}

私有类one2扩展的AsyncTask<字符串,整数,字符串> {

    @覆盖
    保护字符串doInBackground(字符串... PARAMS){
        // TODO自动生成方法存根
        //初始化SOAP请求+添加参数
  SoapObject请求=新SoapObject(命名空间METHOD_NAME2);

  //使用它来添加参数
  request.addProperty(摄氏度,txtCel.getText()的toString());

  //声明SOAP请求的版本
  SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);

  envelope.setOutputSoapObject(要求);
  envelope.dotNet = TRUE;

  尝试 {
        HttpTransportSE androidHttpTransport =新HttpTransportSE(URL);

        //这是将调用Web服务的实际组成部分
        androidHttpTransport.call(SOAP_ACTION2,包);

        //从信封体在SOA presult。
        SoapObject结果=(SoapObject)envelope.bodyIn;
        如果(结果!= NULL)
        {
              //获取第一属性,并更改标签文本
              txtFar.setText(result.getProperty(0)的ToString());

        }
        其他
        {

              Toast.makeText(getApplicationContext(),不回应,Toast.LENGTH_LONG).show();

        }
  }赶上(例外五){
        e.printStackTrace();
  }返回null;
    }

}
}
 

错误日志和code

 私有类one1扩展的AsyncTask<字符串,整数,SoapObject> {

保护SoapObject doInBackground(字符串...为arg0){

保护** **无效于preExecute(**)**
 {
     //从TextView中/的EditText值
 }

// TODO自动生成方法存根
   //初始化SOAP请求+添加参数
SoapObject请求=新SoapObject(命名空间METHOD_NAME1);

//使用它来添加参数
request.addProperty(华氏,30); //这里提供的价值

//声明SOAP请求的版本
SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(要求);
envelope.dotNet = TRUE;

尝试 {
      HttpTransportSE androidHttpTransport =新HttpTransportSE(URL);
      androidHttpTransport.call(SOAP_ACTION1,包);
      **结果** =(SoapObject)envelope.bodyIn;

}赶上(例外五){

      e.printStackTrace();

}
返回** **的结果;
   }

   保护无效onPostExecute(SoapObject结果)
   {
如果(结果!= NULL)
{
  的System.out.println(result.getProperty(0)的ToString());
}
其他
{
  Toast.makeText(getApplicationContext(),不回应,Toast.LENGTH_LONG).show();
}
}

}
}
 

语法错误,将枚举标识符完成EnumHeader 令牌作废,@预计
语法错误 结果不能被解析为一个变量FirstScreen.java
结果不能被解析为一个变量FirstScreen.java

编辑code:

 包com.webservice;

进口org.ksoap2.SoapEnvelope;
进口org.ksoap2.serialization.SoapObject;
进口org.ksoap2.serialization.SoapSerializationEnvelope;
进口org.ksoap2.transport.HttpTransportSE;

进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
进口android.widget.Toast;



公共类FirstScreen扩展活动
{


     私有静态字符串SOAP_ACTION1 =htt​​p://tempuri.org/FahrenheitToCelsius;
     私有静态字符串NAMESPACE =htt​​p://tempuri.org/;
     私有静态字符串METHOD_NAME1 =FahrenheitToCelsius;
     私有静态字符串URL =htt​​p://www.w3schools.com/webservices/tempconvert.asmx?WSDL;
     SoapObject结果;
     按钮B;
     的EditText等;
     int值;
     TextView的电视;
     ProgressDialog PD;

     @覆盖
      公共无效的onCreate(包savedInstanceState)
      {

        super.onCreate(savedInstanceState);
        PD =新ProgressDialog(本);
        pd.setTitle(使SOAP请求);
        的setContentView(R.layout.activity_first_screen);
        B =(按钮)findViewById(R.id.button1);
        电视=(TextView中)findViewById(R.id.textView2);
        等=(EditText上)findViewById(R.id.ed);
                b.setOnClickListener(新OnClickListener()
                {

               @覆盖
               公共无效的onClick(视图v){
               // TODO自动生成方法存根
                值=的Integer.parseInt(et.getText()的toString());
                新one1()执行()。
        }
    });


    }

     私有类one1扩展的AsyncTask<字符串,整数,SoapObject> {

      在preExecute保护无效()
      {
          pd.show();
      }
    保护SoapObject doInBackground(字符串...为arg0){
        // TODO自动生成方法存根
           //初始化SOAP请求+添加参数
        SoapObject请求=新SoapObject(命名空间METHOD_NAME1);

        //使用它来添加参数
        request.addProperty(华氏,值);

        //声明SOAP请求的版本
        SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(要求);
        envelope.dotNet = TRUE;

        尝试 {
              HttpTransportSE androidHttpTransport =新HttpTransportSE(URL);
              androidHttpTransport.call(SOAP_ACTION1,包);
              结果=(SoapObject)envelope.bodyIn;

        }赶上(例外五){

              e.printStackTrace();

        }
        返回结果;
    }

     保护无效onPostExecute(SoapObject结果)
     {
      pd.dismiss();
    如果(结果!= NULL)
    {
          tv.setText(结果=+ result.getProperty(0)的ToString());
    }
    其他
    {
          Toast.makeText(getApplicationContext(),不回应,Toast.LENGTH_LONG).show();
    }
    }

    }
}
 

解决方案

您在后台线程更新UI。你必须更新的用户界面UI线程。

返回结果doInBackground()

在onPostExecute()更新用户界面。

 虚空onPostExecute(INT结果)
   {
       如果(结果!= NULL)
    {
          txtFar.setText(result.getProperty(0)的ToString());

    }
    其他
    {

     Toast.makeText(getApplicationContext(),不回应,Toast.LENGTH_LONG).show();

    }
 }
 

http://developer.android.com/reference/android/os/ AsyncTask.html 。下看链接尤其是话题的 4步骤

在你的活动。

 新的SOA prequestTask()执行(); //调用从UI asyntask。


私营级SOA prequestTask延伸的AsyncTask<虚空,虚空龙> {

 在preExecute保护无效(){
     //显示进度对话框
 }

 保护龙doInBackground(空... PARAMS){

   //使SOAP请求,并得到结果。这里不更新UI。
     返回结果;
 }



 保护无效onPostExecute(长效){
     //驳回对话框。更新的用户界面在这里。
 }
}
 

http://10keyes.blogspot.in/ 2012/08 / Android的实施,ksoap2功能于asynctask.html 。 AsyncTask的和KSOAP的教程。

修改

 公共类FirstScreen扩展活动
 {
 私有静态字符串SOAP_ACTION1 =htt​​p://tempuri.org/FahrenheitToCelsius;
 私有静态字符串NAMESPACE =htt​​p://tempuri.org/;
 私有静态字符串METHOD_NAME1 =FahrenheitToCelsius;
 私有静态字符串URL =htt​​p://www.w3schools.com/webservices/tempconvert.asmx?WSDL;
SoapObject结果;
 按钮B;
 的EditText等;
 int值;
 TextView的电视;
 ProgressDialog PD;

 @覆盖
  公共无效的onCreate(包savedInstanceState)
  {

    super.onCreate(savedInstanceState);
    PD =新ProgressDialog(本);
    pd.setTitle(使SOAP请求);
    的setContentView(R.layout.activity_main);
    B =(按钮)findViewById(R.id.button1);
    电视=(TextView中)findViewById(R.id.textView2);
    等=(EditText上)findViewById(R.id.ed);
    b.setOnClickListener(新OnClickListener()
    {

        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            值=的Integer.parseInt(et.getText()的toString());
            新one1()执行()。
        }

    });


}

 私有类one1扩展的AsyncTask<字符串,整数,SoapObject> {

  在preExecute保护无效()
  {
      pd.show();
  }
保护SoapObject doInBackground(字符串...为arg0){
    // TODO自动生成方法存根
       //初始化SOAP请求+添加参数
    SoapObject请求=新SoapObject(命名空间METHOD_NAME1);

    //使用它来添加参数
    request.addProperty(华氏,值);

    //声明SOAP请求的版本
    SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(要求);
    envelope.dotNet = TRUE;

    尝试 {
          HttpTransportSE androidHttpTransport =新HttpTransportSE(URL);
          androidHttpTransport.call(SOAP_ACTION1,包);
          结果=(SoapObject)envelope.bodyIn;

    }赶上(例外五){

          e.printStackTrace();

    }
    返回结果;
}

 保护无效onPostExecute(SoapObject结果)
 {
  pd.dismiss();
如果(结果!= NULL)
{
      tv.setText(结果=+ result.getProperty(0)的ToString());
}
其他
{
      Toast.makeText(getApplicationContext(),不回应,Toast.LENGTH_LONG).show();
}
}

}
 

我activity_main.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
工具:上下文=MainActivity。>

<的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:文本=FahrenheitToCelsius/>

<的EditText
    机器人:ID =@ + ID / ED
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =43dp/>

<按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:layout_marginTop =37dp
    机器人:文本=MakeRequest/>

<的TextView
    机器人:ID =@ + ID / textView2
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:layout_marginTop =20dp
    机器人:文本=结果/>

 < / LinearLayout中>
 

这是我的电话的结果。

I used the tutorial http://www.c-sharpcorner.com/UploadFile/88b6e5/how-to-call-web-service-in-android-using-soap/ and did some chances that it shoud work for android 4.0.3! But it still doesn't! Can someone post me a correct solution?

package com.webservice;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class FirstScreen extends Activity

{
/** Called when the activity is first created. */

private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME1 = "FahrenheitToCelsius";
private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar,btnCel,btnClear;
EditText txtFar,txtCel;

@Override
public void onCreate(Bundle savedInstanceState)

{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_screen); 
        btnFar = (Button)findViewById(R.id.btnFar);
        btnCel = (Button)findViewById(R.id.btnCel);
        btnClear = (Button)findViewById(R.id.btnClear);
        txtFar = (EditText)findViewById(R.id.txtFar);
        txtCel = (EditText)findViewById(R.id.txtCel);

        btnFar.setOnClickListener(new View.OnClickListener()
        {
                 @Override
                  public void onClick(View v)
                  {
                      new one1().execute();
                  }
            });            
        btnCel.setOnClickListener(new View.OnClickListener()
        {
                  @Override
                  public void onClick(View v)
                  {
                      new one2().execute();
                  }
            });

        btnClear.setOnClickListener(new View.OnClickListener()
        {
                  @Override
                  public void onClick(View v)
                  {
                        txtCel.setText("");
                        txtFar.setText("");
                  }
            });     
 }

private class one1 extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
           //Initialize soap request + add parameters
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);    

        //Use this to add parameters
        request.addProperty("Fahrenheit",txtFar.getText().toString());         

        //Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);          

        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;         

        try {
              HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);                

              //this is the actual part that will call the webservice
              androidHttpTransport.call(SOAP_ACTION1, envelope);

              // Get the SoapResult from the envelope body.
              SoapObject result = (SoapObject)envelope.bodyIn;
              if(result != null)
              {
                    //Get the first property and change the label text
                    txtCel.setText(result.getProperty(0).toString());
              }
              else
              {
                    Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
              }
        } catch (Exception e) {

              e.printStackTrace();

        }           
        return null;
    }

}

private class one2 extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        //Initialize soap request + add parameters
  SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);     

  //Use this to add parameters
  request.addProperty("Celsius",txtCel.getText().toString());     

  //Declare the version of the SOAP request
  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);    

  envelope.setOutputSoapObject(request);
  envelope.dotNet = true;   

  try {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);          

        //this is the actual part that will call the webservice
        androidHttpTransport.call(SOAP_ACTION2, envelope);          

        // Get the SoapResult from the envelope body.            
        SoapObject result = (SoapObject)envelope.bodyIn;            
        if(result != null)
        {
              //Get the first property and change the label text
              txtFar.setText(result.getProperty(0).toString());

        }
        else
        {

              Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();

        }
  } catch (Exception e) {
        e.printStackTrace();
  }         return null;
    }

}
}

Errorlog and Code

private class one1 extends AsyncTask<String, Integer, SoapObject> {

protected SoapObject doInBackground(String... arg0) {

protected **void** onPreExecute(**)**
 {
     //value from textview/edittext
 }

// TODO Auto-generated method stub
   //Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);    

//Use this to add parameters
request.addProperty("Fahrenheit","30");// provide value here        

//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);          

envelope.setOutputSoapObject(request);
envelope.dotNet = true;         

try {
      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);                
      androidHttpTransport.call(SOAP_ACTION1, envelope);
      **result** = (SoapObject)envelope.bodyIn;

} catch (Exception e) {

      e.printStackTrace();

}           
return **result**;
   }

   protected void onPostExecute(SoapObject result)
   {
if(result != null)
{
  System.out.println(result.getProperty(0).toString());
}
else
{
  Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
}

}
}

Syntax error, insert "enum Identifier" to complete EnumHeader Syntax error on token "void", @ expected
result cannot be resolved to a variable FirstScreen.java
result cannot be resolved to a variable FirstScreen.java

Edited Code :

package com.webservice;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



public class FirstScreen extends Activity
{


     private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
     private static String NAMESPACE = "http://tempuri.org/";
     private static String METHOD_NAME1 = "FahrenheitToCelsius";
     private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
     SoapObject result;
     Button b;
     EditText et;
     int value;
     TextView tv;
     ProgressDialog pd;

     @Override
      public void onCreate(Bundle savedInstanceState)
      {

        super.onCreate(savedInstanceState);
        pd= new ProgressDialog(this);
        pd.setTitle("Making Soap Request");
        setContentView(R.layout.activity_first_screen); 
        b= (Button) findViewById(R.id.button1);
        tv= (TextView) findViewById(R.id.textView2);
        et= (EditText) findViewById(R.id.ed);           
                b.setOnClickListener(new OnClickListener()
                {

               @Override
               public void onClick(View v) {
               // TODO Auto-generated method stub
                value=Integer.parseInt(et.getText().toString());
                new one1().execute();
        }
    });


    }

     private class one1 extends AsyncTask<String, Integer, SoapObject> {

      protected void onPreExecute()
      {
          pd.show();
      }
    protected SoapObject doInBackground(String... arg0) {
        // TODO Auto-generated method stub
           //Initialize soap request + add parameters
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);    

        //Use this to add parameters
        request.addProperty("Fahrenheit",value);         

        //Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);          

        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;         

        try {
              HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);                
              androidHttpTransport.call(SOAP_ACTION1, envelope);
              result = (SoapObject)envelope.bodyIn;

        } catch (Exception e) {

              e.printStackTrace();

        }           
        return result;
    }

     protected void onPostExecute(SoapObject result)
     {
      pd.dismiss();
    if(result != null)
    {
          tv.setText("Result = "+result.getProperty(0).toString());
    }
    else
    {
          Toast.makeText(getApplicationContext(), "No       Response",Toast.LENGTH_LONG).show();
    }
    }

    }
}

解决方案

You are updating UI in the background thread. You have to update UI on the UI thread.

Return result in doInBackground()

In onPostExecute() update ui.

   Void onPostExecute(int result)  
   {
       if(result != null)
    {
          txtFar.setText(result.getProperty(0).toString());

    }
    else
    {

     Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();

    }
 }

http://developer.android.com/reference/android/os/AsyncTask.html. Look at the link especially the topic under The 4 steps.

In your Activity.

new SoapRequestTask().execute();//call asyntask from ui.


private class SoapRequestTask extends AsyncTask<Void, Void, Long> {

 protected void onPreExecute() {
     //display progress dialog
 }

 protected Long doInBackground(Void... params) {

   //make soap request and get result. do not update ui here.
     return result;
 }



 protected void onPostExecute(Long result) {
     //dismiss dialog. update ui here.
 }
}

http://10keyes.blogspot.in/2012/08/android-implement-ksoap2-in-asynctask.html. A tutorial of asynctask and ksoap.

Edit

 public class FirstScreen extends Activity
 {
 private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
 private static String NAMESPACE = "http://tempuri.org/";
 private static String METHOD_NAME1 = "FahrenheitToCelsius";
 private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
SoapObject result;
 Button b;
 EditText et;
 int value;
 TextView tv;
 ProgressDialog pd;

 @Override
  public void onCreate(Bundle savedInstanceState)
  {

    super.onCreate(savedInstanceState);
    pd= new ProgressDialog(this);
    pd.setTitle("Making Soap Request");
    setContentView(R.layout.activity_main); 
    b= (Button) findViewById(R.id.button1);
    tv= (TextView) findViewById(R.id.textView2);
    et= (EditText) findViewById(R.id.ed);
    b.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            value=Integer.parseInt(et.getText().toString());
            new one1().execute();
        }

    });


}

 private class one1 extends AsyncTask<String, Integer, SoapObject> {

  protected void onPreExecute()
  {
      pd.show();
  }
protected SoapObject doInBackground(String... arg0) {
    // TODO Auto-generated method stub
       //Initialize soap request + add parameters
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);    

    //Use this to add parameters
    request.addProperty("Fahrenheit",value);         

    //Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);          

    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;         

    try {
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);                
          androidHttpTransport.call(SOAP_ACTION1, envelope);
          result = (SoapObject)envelope.bodyIn;

    } catch (Exception e) {

          e.printStackTrace();

    }           
    return result;
}

 protected void onPostExecute(SoapObject result)
 {
  pd.dismiss();
if(result != null)
{
      tv.setText("Result = "+result.getProperty(0).toString());
}
else
{
      Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
}

}

My activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="FahrenheitToCelsius" />

<EditText
    android:id="@+id/ed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="43dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="37dp"
    android:text="MakeRequest" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:text="Result" />

 </LinearLayout>

Result on my phone.

这篇关于要如果曾与AsynxTasc工作,但我仍然不工作,使用教程安卓4.0.3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆