问题描述
喜是新的Android java开发,并想从我的Android的Java应用程序调用Java web服务。当我试图运行该应用程序我得到一个例外,它说android.os.NetworkOnMainThreadException。我不知道为什么这个错误..?反正我坚持了这个错误。我曾经用Google搜索这个有很多,但无法找到一个确切的solution..can有人plz帮助..?我的code以下片断。
Hi am new to Android java development and am trying to call a java webservice from my android java application. When am trying to run this application am getting one exception which says android.os.NetworkOnMainThreadException. I didn't know why this error..?? anyway am stuck with this error. I had googled for this a lot but can't find an exact solution..can anyone plz help..?my code snippet below.
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION,envelope);
SoapObject so = (SoapObject)envelope.bodyIn;
完成调试,我认为code androidHttpTransport.call(SOAP_ACTION,信封),这些行后;
正在的问题。
SoapObject所以=(SoapObject)envelope.bodyIn;
在code envelope.bodyIn
给我一个空值。
After done the debugging i think these lines of code androidHttpTransport.call(SOAP_ACTION,envelope); SoapObject so = (SoapObject)envelope.bodyIn;
is making problems.The code envelope.bodyIn
giving me a null value.
推荐答案
您可以使用AsyncTask的,这将prevent创建您自己的线程的麻烦。使doInBackground方法网络呼叫
You can use the AsyncTask, which will prevent the hassle of creating your own thread. Make network call in doInBackGround method
code
public void onClick(View v) {
new DownloadImageTask().execute(data);
}
class Data {
String namespace;
String method;
}
private class DownloadImageTask extends AsyncTask<data, Void, SoapObject> {
protected SoapObject doInBackground(Data... data) {
//make your network call and return SoapObject
}
protected void onPostExecute(SoapObject obj) {
//process the Soap Object returned
}
}
这篇关于android.os.NetworkOnMainThreadException例外尝试从我的Android的Java应用程序调用web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!