ksoap 2通信 - 将asynctask结果发送到UI线程并打印

时间:2013-04-30 03:41:51

标签: android asp.net web-services android-asynctask android-ksoap2

我正在尝试通过visual studio将Android用户的登录凭据发送到在我的本地主机上运行的C#Web服务。我使用KSOAP2进行SOAP通信,并希望将登录信息发送到Web服务并在UI中打印。我正在使用AsyncTask来执行此操作,但无法从Web服务获取信息并将其打印到Android UI。

有人可以在我出错的地方帮忙吗?????

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;

}

错误信息:

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

3 个答案:

答案 0 :(得分:1)

首先,您需要分配这些变量:

`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和SOAP_ACTION不能为空。以下是您需要为项目提供的值:

<SOAP_ACTION="http://spartansparking.org/login";>
<METHOD_NAME="login";>
<NAMESPACE="http://spartansparking.org/";>
<URL = "http://localhost:63734/parkingservice.asmx";>

要调用WebService .NET,您实际上不需要使用AsyncTask。您可以使用用户主线程。

答案 1 :(得分:0)

如果您在Emulator上工作,本地主机尝试将10.0.2.2用作本地主机 喜欢: set URL =“http://10.0.2.2/parkingservice.asmx”;&gt;

答案 2 :(得分:0)

如果你得到java.net.SocketException: socket failed: EACCES (Permission denied)

也许你应该检查你的许可:

<uses-permission android:name="android.permission.INTERNET" />