gpt4 book ai didi

android - 如何从android中的Web服务获取数据?

转载 作者:行者123 更新时间:2023-11-29 14:51:10 25 4
gpt4 key购买 nike

我是 Android 开发的新手,我正在开发一个可以从 Web 服务获取数据的应用程序。例如,我在我的 android 应用程序上创建了一个登录名,将要输入它们的数据来自 Web 服务。我该怎么做?使用 JSON 或 SOAP 最简单的方法是什么?请提供一些示例,以便我了解如何执行此操作,谢谢。

最佳答案

这是一个很好的例子,我正在使用类似的东西: enter link description here

private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";

public void getFahrenheit(String celsius) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Property which holds input parameters
PropertyInfo celsiusPI = new PropertyInfo();
//Set Name
celsiusPI.setName("Celsius");
//Set Value
celsiusPI.setValue(celsius);
//Set dataType
celsiusPI.setType(double.class);
//Add the property to request object
request.addProperty(celsiusPI);
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
fahren = response.toString();

} catch (Exception e) {
e.printStackTrace();
}

关于android - 如何从android中的Web服务获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17715449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com