问题描述
限时送ChatGPT账号..我正在试验 android 到网络服务的连接.为了做到这一点,我所做的步骤是:
Hi I am experimenting on android to web service connection. In order to do this, the steps I have done are:
查找免费网络服务:http://services.aonaware/DictService/DictService.asmx
在 Android Studio 上创建一个基本的 Activity
create a basic activity on Android Studio
通过添加以下行来更改 Manifest 文件以获得使用 Internet 的权限:
change the Manifest file to have permission to use internet by adding the following line:
使用权限 android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.INTERNET"
添加 ksoap2 库
add the ksoap2 library
尝试使用以下代码使用 Web 服务:
Try to consume the web service using the following code:
声明
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo property = new PropertyInfo();
property.setName("word");
//property.setType(String.class);
property.setValue("computer");
request.addProperty(property);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
Object response = null;
try {
androidHttpTransport.getServiceConnection();
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
tv.setText("success");
response = envelope.getResponse();
response.toString();
String resultData= response.toString();
tv.setText(resultData);
} catch (Exception e) {
e.printStackTrace();
}
}
然而,textView1 没有得到结果.我的代码有问题吗?
However, textView1 is not getting the result. Is there anything wrong with my code?
注意:我使用带有 Gradle 2.10 版的 Android Studio
Note: I use Android Studio with Gradle version 2.10
推荐答案
尝试使用 AsyncTask 来执行 Web 服务.这是一个示例 -> android.os.NetworkOnMainThreadException for webservice (ksoap)
Try using AsyncTask for executing Webservices. Here's a sample -> android.os.NetworkOnMainThreadException for webservice (ksoap)
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;
public class MainActivity extends Activity {
private String TAG ="Vik";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "doInBackground");
calculate();
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
}
public void calculate()
{
String SOAP_ACTION = "http://tempuri/CelsiusToFahrenheit";
String METHOD_NAME = "CelsiusToFahrenheit";
String NAMESPACE = "http://tempuri/";
String URL = "http://www.w3schools/webservices/tempconvert.asmx";
try {
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport= new HttpTransportSE(URL);
transport.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
Log.i(TAG, "Result Celsius: " + resultString);
}
catch(Exception ex) {
Log.e(TAG, "Error: " + ex.getMessage());
}
SOAP_ACTION = "http://tempuri/FahrenheitToCelsius";
METHOD_NAME = "FahrenheitToCelsius";
try {
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Fahrenheit", "100");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport= new HttpTransportSE(URL);
transport.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
Log.i(TAG, "Result Fahrenheit: " + resultString);
}
catch(Exception ex) {
Log.e(TAG, "Error: " + ex.getMessage());
}
}
}
这篇关于无法将安卓连接到网络服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论