我的目标是从Android设备调用.Net webservice(.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")
行上收到“源未找到”消息
(检查构建路径,有对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...
}
}
}
答案 0 :(得分:0)
我最好的猜测是你在ICS上测试这个解决方案。
您可以快速测试使用Strick模式:http://developer.android.com/reference/android/os/StrictMode.html。
在ICS中禁止在UI上执行所有网络操作,这就是为什么需要重建它以使用AsyncTasks http://developer.android.com/reference/android/os/AsyncTask.html。
答案 1 :(得分:0)
您无法在主线程上执行网络操作。结帐
http://developer.android.com/reference/android/os/AsyncTask.html
答案 2 :(得分:0)
只需使用以下代码覆盖......
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build(); StrictMode.setThreadPolicy(政策);
并且还使用......