带有android.os.NetworkOnMainThreadException错误的Webservice客户端
我的目标是从Android设备调用.Net Web服务(.asmx)。
.Net Webservice使用.net测试客户端并使用SOAPUI应用程序进行测试。
我使用Eclipse来构建android应用程序 - 包含用于webservice调用的KSOAP2项目。 应用程序已将android.permission.INTERNET
标记包含在用户权限部分中。
我有两个问题
[1]如果我从onCreate()
(下面的代码)调用webservice方法,应用程序会抛出“android.os.NetworkOnMainThreadException”错误(我在互联网上看到的许多示例都有相同的方法,但我不知道为什么在我的项目中失败)
[2]为了解决上述问题,我在“MainActivity”内部创建了一个类,但现在我在线client.execute("some text as input")
上收到了“source not found”消息(检查构建路径,到KSOAP2 jar文件并且没有编译错误)
什么是正确的方法,请问如何解决这个问题? 谢谢你的帮助
[问题1]的代码
//This code for calling .net webservice from Android devide.. and throws "android.os.NetworkOnMainThreadException" whilst calling.
package com.example.wsclient;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String SOAP_ACTION = "http://mycomp.org/GetParticipantVote";
String METHOD_NAME = "GetParticipantVote";
String NAMESPACE = "http://mycomp.org/";
String URL = "http://10.0.2.2:8080/WSRating.asmx";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("sID", "My_name");
request.addProperty("sRate", "9");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE (URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result=(SoapObject)envelope.getResponse();
//To get the data.
String resultData=result.getProperty(0).toString();
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Webservice Client");
alertDialog.setMessage("Webservice Resp : " + resultData);
alertDialog.show();
}
catch (Exception eE) {
AlertDialog alertDialog1 = new AlertDialog.Builder(this).create();
alertDialog1.setTitle("Webservice Client");
alertDialog1.setMessage("Error : " + eE.getMessage());
alertDialog1.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
[问题2]的代码 -
package com.example.wsclient;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CallWS client = new CallWS();
client.execute("some text as input");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/* AsyncTask class to remove android.os.NetworkOnMainThreadException error */
private class CallWS extends AsyncTask<String, Integer, String> {
protected String doInBackground(String... sValues) {
String resultData;
String SOAP_ACTION = "http://mycomp.org/GetParticipantVote";
String METHOD_NAME = "GetParticipantVote";
String NAMESPACE = "http://mycomp.org/";
String URL = "http://10.0.2.2:8080/WSRating.asmx";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("sID", "My_name");
request.addProperty("sRate", "9");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE (URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result=(SoapObject)envelope.getResponse();
//To get the data.
resultData =result.getProperty(0).toString();
}
catch (Exception eE) {
resultData = "Error : " + eE.getMessage();
}
return resultData
}
protected void onProgressUpdate(Integer... values) {
}
protected void onPostExecute(String result) {
// do something here to display the result...
}
}
}
我最好的猜测是你正在ICS上测试这个解决方案。
你可以为快速测试purpouses使用Strick模式:http://developer.android.com/reference/android/os/StrictMode.html。
在ICS中,UI上的所有网络操作都是禁止的,这就是为什么你需要重建它以使用AsyncTasks,http://developer.android.com/reference/android/os/AsyncTask.html。
您不能在主线程上执行网络操作。 查看
http://developer.android.com/reference/android/os/AsyncTask.html
只需使用以下代码覆盖...
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build(); StrictMode.setThreadPolicy(政策);
也用...
链接地址: http://www.djcxy.com/p/91875.html上一篇: Webservice client with android.os.NetworkOnMainThreadException error