开发者

how to use handler to run a task in background

开发者 https://www.devze.com 2023-04-09 19:01 出处:网络
I am using the following code to to access web service and it\'s showing me an error. The application is not responding.

I am using the following code to to access web service and it's showing me an error. The application is not responding.

package com.android.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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class WebServiceActivity extends Activity {
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";

    private static final String METHOD_NAME = "HelloWorld";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        Button getquote = (Button) findViewById(R.id.getquote);  
        getquote.setOnClickListener(new OnClickListener() {  

        public void onClick(View v) { 
            TextView result1;
            result1=(TextView)findViewById(R.id.result1);
        try {


            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            EditText CompanyName = (EditText) findViewById(R.id.CompanyName); 
            String val1 = (CompanyName.getText().toString());
            request.addProperty("passonString", val1);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = (Object)envelope.getResponse();

            result1.setText(result.toString());
        } catch (开发者_StackOverflow社区Exception e) {

            result1.setText(e.getMessage());
            }
    }
});
    }
}

May be because this is running in UI thread, I want to run it in background using handler. I am new in this field so I'm having problems to write it in background thread. Can anyone please give me directions on how to write the code inside handler? Thanks


try this:: AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute

     private class xyz extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(tranning.this);

    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Please Wait...");
        this.dialog.show();
        // put your code which preload with processDialog  
    }


    @Override
    protected Void doInBackground(Void... arg0) {
        // put your code here
        return null;
    }

    @Override
    protected void onPostExecute(final Void unused) {
        if (this.dialog.isShowing()) {
          this.dialog.dismiss();

        }   
    }
}

and use this in main ::

new xyz().execute();

For more Elaboration
. . . . . . . . . .
. . . . . .
. . . .
. .
.


You should Use AysncTask to do this work in background

http://labs.makemachine.net/2010/05/android-asynctask-example/

0

精彩评论

暂无评论...
验证码 换一张
取 消