在android eclipse上从webservice检索特定数据
有人能告诉我如何在android eclipse上从web服务检索特定数据吗。 到目前为止,我已经做了这些事情。请告诉我如何修复这个错误的来源 请在下面找到我的资料来源 [WEB方法]在android eclipse上从webservice检索特定数据,android,eclipse,web-services,Android,Eclipse,Web Services,有人能告诉我如何在android eclipse上从web服务检索特定数据吗。 到目前为止,我已经做了这些事情。请告诉我如何修复这个错误的来源 请在下面找到我的资料来源 [WEB方法] package com.android.backend; public class FahrenheitToCelsius { public double FahrenheitToCelsius(double str){ return ((str-32)*5)/9; } }
package com.android.backend;
public class FahrenheitToCelsius {
public double FahrenheitToCelsius(double str){
return ((str-32)*5)/9;
}
}
package com.android.button.web;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Tesing_webserviceActivity extends Activity
{
/** Called when the activity is first created. */
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "FahrenheitToCelsius";
private static String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar,btnClear;
EditText txtFar,txtres;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.btn_getvalues);
btnClear = (Button)findViewById(R.id.btnClear);
txtFar = (EditText)findViewById(R.id.txtFar);
txtres = (EditText)findViewById(R.id.txtresult);
btnFar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Use this to add parameters
request.addProperty("str",txtFar.getText().toString());
//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);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
//Get the first property and change the label text
txtres.setText(result.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
btnClear.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
txtres.setText("");
txtFar.setText("");
}
});
}
}
[SCREENACTIVITY.JAVA]
package com.android.backend;
public class FahrenheitToCelsius {
public double FahrenheitToCelsius(double str){
return ((str-32)*5)/9;
}
}
package com.android.button.web;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Tesing_webserviceActivity extends Activity
{
/** Called when the activity is first created. */
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "FahrenheitToCelsius";
private static String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar,btnClear;
EditText txtFar,txtres;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.btn_getvalues);
btnClear = (Button)findViewById(R.id.btnClear);
txtFar = (EditText)findViewById(R.id.txtFar);
txtres = (EditText)findViewById(R.id.txtresult);
btnFar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Use this to add parameters
request.addProperty("str",txtFar.getText().toString());
//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);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
//Get the first property and change the label text
txtres.setText(result.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
btnClear.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
txtres.setText("");
txtFar.setText("");
}
});
}
}
非常感谢 如果您要从Web服务检索简单信息,我建议使用返回JSON对象的REST服务。 在Android中,您可以使用GSON库轻松解析这个JSON对象。 将JSON解析为Java对象的示例:
objectType = new TypeToken<YourClass>(){}.getType();
//Parse the respons with GSON
Gson gson = new Gson();
return gson.fromJson(webserviceResponse, objectType);
我在我的一个项目中使用此功能(您可能不需要授权标题):
确切的问题是什么?你得到了什么,你期望得到什么?我有一个包含一些数据的web服务。好的。这里我需要使用android/eclipse的web服务,所以我想通过edittextbox从用户那里获取输入,并根据用户在android eclipse上的输入显示相应的值。