-1

当我试图联系网络服务并向他们发送华氏温度时,它会为我将它们转换为 celsium,但它会返回:

FahrenheitToCelsiusResponse{FahrenheitToCelsiusResult = ERROR;}

package com.test123;

import java.net.SocketException;

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.test123.R;

public class Activity123 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";

      /*private static String SOAP_ACTION = "http://tempuri.org/HelloWorld";
      private static String NAMESPACE = "http://tempuri.org/";
      private static String METHOD_NAME = "HelloWorld";
      private static String URL = "http://192.168.0.25/webapplication1/ws.asmx";*/

      Button button_to_f,button_to_c,button_clear;
      EditText text_f,text_c;

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

            button_to_f = (Button)findViewById(R.id.button_to_f);
            button_to_c = (Button)findViewById(R.id.button_to_f);
            button_clear = (Button)findViewById(R.id.button_clear);
            text_f = (EditText)findViewById(R.id.text_f);
            text_c = (EditText)findViewById(R.id.text_c);
         }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) 
         {
            getMenuInflater().inflate(R.menu.activity_activity123, menu);
            return true;
         }

      public void F_TO_C(View button_to_f)
         {
            new CelsiumToFahrenheit().execute(""); 
            //text_f.setText( "zagonAsynTask");
         }

      private class CelsiumToFahrenheit extends AsyncTask<String, Void, String> 
         {
            @Override
            protected String doInBackground(String... params) 
               {
                  SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1); // Zmeraj je že tuki program se ustavu, dons pa kr po čudežu NIKJER se ne ustav...                      
                  final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);     

                  envelope.dotNet = true;
                  envelope.encodingStyle = SoapSerializationEnvelope.XSD;
                  envelope.setOutputSoapObject(request);

                  request.addProperty("Celsius",text_c.getText().toString() );

                  final HttpTransportSE  HT = new HttpTransportSE(URL);
                  //Allow for debugging - needed to output the request
                  //HT.debug = true;

                  try 
                     {
                        HT.call(SOAP_ACTION1, envelope);

                        //final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
//                      Object result = (Object)envelope.getResponse(); 
                        final SoapObject result = (SoapObject)envelope.bodyIn;

                        // Get the SoapResult from the envelope body.
                        //return "Executed:"+response.toString();
                        return result.toString();
                     }
                  catch (Exception e) // Tukaj je blo InterruptedException e
                     {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        return "Napaka:" + e.toString();
                     }               
              }      

            @Override
            protected void onPostExecute(String result) 
               {
                  text_f.setText(result); // txt.setText(result);
                  // might want to change "executed" for the returned string passed into onPostExecute() but that is upto you
               }

            @Override
            protected void onPreExecute() 
               {
                //Empty
               }

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

      public void CLEAR(View v)
         {
            text_c.setText("");
            text_f.setText("");
         }

   }
4

3 回答 3

1

为 EditTexts 创建一个全局引用

喜欢

public EditText myEditText;

然后在onCreate中,将它们分配给真正的EditText

@Override
public void onCreate(Bundle savedInstanceState) {
       .
       .
       setContentView(...)
       myEditText = (EditText) findViewById (R.id.myedittextid);
       .
}

现在在您的工作线程中,您可以使用 myEditText.getText().toString() 来获取 EditText 中的值。

于 2012-10-26T07:46:56.863 回答
0

你为什么不直接使用runOnUiThread(). 您将在提供的链接中找到说明。

此方法将帮助您从工作线程运行和操作,并将其应用于 UI 线程

于 2012-10-26T08:10:11.287 回答
0

愚蠢的错误,更改
request.addProperty("Celsius",text_c.getText().toString() );
request.addProperty("Fahrenheit",text_c.getText().toString() );

我的代码

public class Sample extends Activity {
/** Called when the activity is first created. */
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";
EditText text_c, text_f;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    text_c = new EditText(this);
    text_f = new EditText(this);
    text_c.setText("76");
    new CelsiumToFahrenheit().execute();
}

private class CelsiumToFahrenheit extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
        request.addProperty("Fahrenheit", "78");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        System.setProperty("http.keepAlive", "true");
        try {
            androidHttpTransport.call(SOAP_ACTION1, envelope);
            androidHttpTransport.debug = true;
            Object result = envelope.getResponse();
            System.out.println("GetDataFromNetwork.doInBackground()" + result);
        } catch (IOException e) {
            // e.printStackTrace();
        } catch (XmlPullParserException e) {
            // e.printStackTrace();
        }
        return null;

    }

    @Override
    protected void onPostExecute(String result) {
        text_f.setText(result); // txt.setText(result);
    }
}

}

于 2012-10-27T08:58:29.220 回答