我正在运行一个简单的项目,点击 android 中的按钮。显示一条从 web 服务返回的消息,该 web 服务的编码是在 java 中完成的。
这是我在 eclipse for Android 中的程序:
@Override
public void onClick(View v) {
final String url = "http://10.0.2.2:8050/WebPractice/PracticeWebServices?wsdl";
final String namespace = "http://PracticeWebServices/";
final String methodName = "hello";
final String action = namespace + methodName;
if(v.getId() == submit_btn.getId()) {
SoapObject so = new SoapObject(namespace, methodName);
so.addProperty("name", "Arjun");
SoapSerializationEnvelope sse = new SoapSerializationEnvelope(SoapEnvelope.VER11);
sse.setOutputSoapObject(so);
HttpTransportSE hse = new HttpTransportSE(url);
try {
hse.call(action, sse);
SoapPrimitive primitive = (SoapPrimitive)sse.getResponse();
Toast.makeText(getApplicationContext(),primitive.toString() , Toast.LENGTH_SHORT);
} catch(IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}
以下是我的 Java 代码:
@WebService(serviceName = "PracticeWebServices")
public class PracticeWebServices {
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String name) {
return "Hello " + name + " !";
}
}
我已经在清单文件中包含了互联网权限......
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
现在,当我在 Eclipse 提供的模拟器中运行该文件时。单击按钮时,它会向我显示一条消息Unfortunately the program has stopped
当我检查 logCat....似乎我在android.os.NetworkOnMainThreadException遇到异常