-1

When im trying to contact web service and send them Fahrenheit, so that it converts them for me into celsium, but instead it returns:

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("");
         }

   }
Code_Life
  • 5,742
  • 4
  • 29
  • 49
Kilo
  • 19
  • 2

3 Answers3

1

Create a global reference for the EditTexts

like

public EditText myEditText;

and then in onCreate, assign them to the real EditText

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

now inside your worker thread, you can use myEditText.getText().toString() to get the value inside the EditText.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Soham
  • 4,940
  • 3
  • 31
  • 48
  • But doesn't that make worker thread to use UI thread by doing that and making the app crash ? It does crash... im asking this question to get values in need into the class before the worker thread starts, just before my envelope gets finished... – Kilo Oct 26 '12 at 08:06
  • 1
    I don't think that would mean it is part of the UI thread just access to UI assets. If you called something in edittext that required UI changes though I think it would crash. If you are worried about that and want to stay safe then you could just make global Strings for the edittext values and change them whenever edittexts are changed (using addTextChangedListener) – MikeIsrael Oct 26 '12 at 08:11
  • Ill use that, but first... WHY does one get ERROR as the response... And how do you fix it ? – Kilo Oct 26 '12 at 08:46
  • Response from server is ERROR. – Kilo Oct 26 '12 at 09:07
0

why don't you just use runOnUiThread(). You'll find the description in the provided link..

this method will help run and action from your worker thread and apply it to the UI thread

ColdFire
  • 6,764
  • 6
  • 35
  • 51
0

Stupid Error , Change
request.addProperty("Celsius",text_c.getText().toString() );
to request.addProperty("Fahrenheit",text_c.getText().toString() );

My code

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

}

Code_Life
  • 5,742
  • 4
  • 29
  • 49