gpt4 book ai didi

android - 无法将 Android 连接到 Web 服务

转载 作者:行者123 更新时间:2023-11-30 01:21:45 25 4
gpt4 key购买 nike

您好,我正在试验 android 到 web 服务的连接。为了做到这一点,我完成的步骤是:

  1. 查找免费网络服务:http://services.aonaware.com/DictService/DictService.asmx

  2. 在 Android Studio 上创建一个基本 Activity

  3. 通过添加以下行更改 Manifest 文件以允许使用互联网:

    使用权限 android:name="android.permission.INTERNET"

  4. 添加ksoap2库

  5. 尝试使用以下代码使用网络服务:

declarations

@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 没有得到结果。我的代码有什么问题吗?

注意:我使用带有 Gradle 版本 2.10 的 Android Studio

最佳答案

尝试使用 AsyncTask 来执行 Web 服务。这是一个示例 -> 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.org/CelsiusToFahrenheit";
String METHOD_NAME = "CelsiusToFahrenheit";
String NAMESPACE = "http://tempuri.org/";
String URL = "http://www.w3schools.com/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.org/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());
}
}

}

关于android - 无法将 Android 连接到 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36985939/

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