0

我做了关于 Android 中 kSoap的教程!

我的代码适用于 Android 2.2,但不适用于 Android 4.0.3!我听说我必须将代码切换到AsyncTask...。现在我不知道该怎么做!有谁能够帮我?

这是我的代码!

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.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) {
                // 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();
                }
            }
        });
        btnCel.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v){
                // 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();
                }
            }
        });

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

现在我用 AsyncTask 试过了,但我还是不行!有什么帮助吗?

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.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;
    }


}
}
4

2 回答 2

0

您所要做的就是创建一个类

private class TaskAsync extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {

            // Do all your connection stuff here

            return "Executed";
      }      

      @Override
      protected void onPostExecute(String result) {
           // here you fetch the data into the UI widgets
      }

      @Override
      protected void onPreExecute() {
      }

      @Override
      protected void onProgressUpdate(Void... values) {
      }
}

然后打电话

new TaskAsynk().execute;

希望它会有所帮助

于 2013-03-18T12:52:24.773 回答
0

在异步类的 doInBackground 方法中编写代码

  private class async extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {

            }

            return "Executed";
      }      

      @Override
      protected void onPostExecute(String result) {

      }

      @Override
      protected void onPreExecute() {
      }

      @Override
      protected void onProgressUpdate(Void... values) {
      }
}  

并单击按钮调用

 new async().execute(); 
于 2013-03-18T12:54:20.377 回答