问题
I am trying to send the login credentials from android user to a C# web service running in my local host through visual studio. I have used KSOAP2 for the SOAP communication and wanted to send the login info to the web service and print it in the UI. I am using AsyncTask to do it but was unable to get the information from the web service and print it to android UI.
Can some one please help where I went wrong ?????
public class MainActivity extends Activity {
EditText un,pw;
TextView tv;
Button test;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button test=(Button) findViewById(R.id.login);
un = (EditText) findViewById(R.id.et_un);
pw = (EditText) findViewById(R.id.et_pw);
test.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
FileAsync fleasyn= new FileAsync(null);
fleasyn.execute();
}
});
}
}
class FileAsync extends AsyncTask<String, String, char[]> {
public FileAsync(String quotes)
{
}
private char[] resultstring;
private TextView tv;
@Override
protected void onProgressUpdate(String... progress) {
}
protected void onPostExecute(String unused) {
tv.setText("Status:"+resultstring);
System.out.println(resultstring);
}
@Override
protected char[] doInBackground(String... params) {
final String NAMESPACE = "http://sparking.org/login";
final String METHOD_NAME = "login";
final String SOAP_ACTION = "http://sparking.org/login";
final String URL = "http://localhost:63734/service.asmx";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(URL);
try {
transport.call(SOAP_ACTION, envelope);
SoapPrimitive resultstring = (SoapPrimitive)envelope.getResponse();
}
catch (Exception e)
{
e.printStackTrace();
}
return resultstring;
}
ERROR MESSAGES:
04-30 04:46:14.689: I/System.out(2453): waiting for debugger to settle...
04-30 04:46:14.885: I/System.out(2453): waiting for debugger to settle...
04-30 04:46:15.086: I/System.out(2453): debugger has settled (1399)
04-30 04:49:00.905: W/System.err(2453): java.net.SocketException: socket failed: EACCES (Permission denied)
04-30 04:49:00.925: W/System.err(2453): at libcore.io.IoBridge.socket(IoBridge.java:583)
04-30 04:49:00.935: W/System.err(2453): at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
04-30 04:49:00.945: W/System.err(2453): at java.net.Socket.checkOpenAndCreate(Socket.java:663)
04-30 04:49:00.955: W/System.err(2453): at java.net.Socket.connect(Socket.java:807)
04-30 04:49:00.975: W/System.err(2453): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
04-30 04:49:00.975: W/System.err(2453): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-30 04:49:01.035: W/System.err(2453): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-30 04:49:01.075: W/System.err(2453): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-30 04:49:01.155: W/System.err(2453): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-30 04:49:01.166: W/System.err(2453): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
04-30 04:49:01.185: W/System.err(2453): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
04-30 04:49:01.185: W/System.err(2453): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
04-30 04:49:01.205: W/System.err(2453): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
04-30 04:49:01.215: W/System.err(2453): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
04-30 04:49:01.225: W/System.err(2453): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
04-30 04:49:01.245: W/System.err(2453): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
04-30 04:49:01.255: W/System.err(2453): at com.example.cmpe220.FileAsync.doInBackground(MainActivity.java:171)
04-30 04:49:01.265: W/System.err(2453): at com.example.cmpe220.FileAsync.doInBackground(MainActivity.java:1)
04-30 04:49:01.275: W/System.err(2453): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-30 04:49:01.285: W/System.err(2453): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-30 04:49:01.305: W/System.err(2453): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-30 04:49:01.325: W/System.err(2453): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-30 04:49:01.345: W/System.err(2453): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-30 04:49:01.345: W/System.err(2453): at java.lang.Thread.run(Thread.java:856)
04-30 04:49:01.365: W/System.err(2453): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
04-30 04:49:01.545: W/System.err(2453): at libcore.io.Posix.socket(Native Method)
04-30 04:49:01.758: W/System.err(2453): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
04-30 04:49:01.765: W/System.err(2453): at libcore.io.IoBridge.socket(IoBridge.java:568)
04-30 04:49:01.777: W/System.err(2453): ... 23 more
04-30 04:50:13.456: W/System.err(2453): java.net.SocketException: socket failed: EACCES (Permission denied)
04-30 04:50:13.465: W/System.err(2453): at libcore.io.IoBridge.socket(IoBridge.java:583)
04-30 04:50:13.476: W/System.err(2453): at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
04-30 04:50:13.485: W/System.err(2453): at java.net.Socket.checkOpenAndCreate(Socket.java:663)
04-30 04:50:13.496: W/System.err(2453): at java.net.Socket.connect(Socket.java:807)
04-30 04:50:13.505: W/System.err(2453): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
04-30 04:50:13.517: W/System.err(2453): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-30 04:50:13.525: W/System.err(2453): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-30 04:50:13.545: W/System.err(2453): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-30 04:50:13.555: W/System.err(2453): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-30 04:50:13.575: W/System.err(2453): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
04-30 04:50:13.585: W/System.err(2453): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
04-30 04:50:13.605: W/System.err(2453): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
04-30 04:50:13.615: W/System.err(2453): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
04-30 04:50:13.625: W/System.err(2453): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
04-30 04:50:13.635: W/System.err(2453): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
04-30 04:50:13.655: W/System.err(2453): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
04-30 04:50:13.665: W/System.err(2453): at com.example.cmpe220.FileAsync.doInBackground(MainActivity.java:171)
04-30 04:50:13.685: W/System.err(2453): at com.example.cmpe220.FileAsync.doInBackground(MainActivity.java:1)
04-30 04:50:13.715: W/System.err(2453): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-30 04:50:13.776: W/System.err(2453): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-30 04:50:13.845: W/System.err(2453): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-30 04:50:13.855: W/System.err(2453): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-30 04:50:13.875: W/System.err(2453): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-30 04:50:13.875: W/System.err(2453): at java.lang.Thread.run(Thread.java:856)
04-30 04:50:13.885: W/System.err(2453): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
04-30 04:50:13.915: W/System.err(2453): at libcore.io.Posix.socket(Native Method)
04-30 04:50:13.925: W/System.err(2453): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
04-30 04:50:13.945: W/System.err(2453): at libcore.io.IoBridge.socket(IoBridge.java:568)
04-30 04:50:13.965: W/System.err(2453): ... 23 more
回答1:
First of all, You need to have these variables assigned:
`final String NAMESPACE = "http://xyz.org/";`
`final String METHOD_NAME="";`
`final String SOAP_ACTION="";`
`final String URL = "http://localhost:63734/mywbservice.asmx";**strong text**`
METHOD_NAME and SOAP_ACTION cannot be empty. Here are the values you need to have for the project:
<SOAP_ACTION="http://spartansparking.org/login";>
<METHOD_NAME="login";>
<NAMESPACE="http://spartansparking.org/";>
<URL = "http://localhost:63734/parkingservice.asmx";>
To call a WebService .NET, you dont need to use AsyncTask actually. You can use the user main thread.
回答2:
If you work on Emulator and local host try to use 10.0.2.2 as local host like: set URL ="http://10.0.2.2/parkingservice.asmx";>
回答3:
If you get java.net.SocketException: socket failed: EACCES (Permission denied)
Maybe you should check your permission :
<uses-permission android:name="android.permission.INTERNET" />
来源:https://stackoverflow.com/questions/16291793/ksoap-2-communication-sending-the-asynctask-result-to-ui-thread-and-print