我正在运行一个简单的项目,点击android中的按钮。显示的消息从webservice返回,其编码是在java中完成的。从android中调用java webservices
这是我在Eclipse项目的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 + " !";
}
}
我已经包括在清单文件中的Internet权限....
<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
'NetworkOnMainThreadException'因为主线程上的网络 –
NetworkOnMainThreadException dups无处不在,[this](http://stackoverflow.com/questions/63 43166/android-os-networkonmainthreadexception)[this2](http://stackoverflow.com/questions/5150637/networkonmainthreadexception) – wtsang02