当我试图联系网络服务并向他们发送华氏温度时,它会将它们转换为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("");
}
}
答案 0 :(得分:1)
为EditTexts
创建一个全局引用喜欢
public EditText myEditText;
然后在onCreate中,将它们分配给真正的EditText
@Override
public void onCreate(Bundle savedInstanceState) {
.
.
setContentView(...)
myEditText = (EditText) findViewById (R.id.myedittextid);
.
}
现在在你的工作线程中,你可以使用myEditText.getText()。toString()来获取EditText中的值。
答案 1 :(得分:0)
为什么不使用runOnUiThread()
。您可以在提供的链接中找到说明..
此方法将帮助您的工作线程运行和操作并将其应用于UI线程
答案 2 :(得分: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);
}
}
}