当我试图联系网络服务并向他们发送华氏温度时,它会为我将它们转换为 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("");
}
}