To use the tutorial in android 4.0.3 if had to work with AsynxTasc but i still dont work?

我只是一个虾纸丫 提交于 2019-11-27 16:32:31

You can not access any UI elements in th DoInBackground() method, as it runs on its own thread, not on the UI thread. You should use

txtFar.setText(result.getProperty(0).toString());

and

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

call in the onPostExecute, or in onProgressChanged() methods.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!