从android主线程外部调用webservice

时间:2013-12-26 11:15:01

标签: c# android multithreading web-services

在我的代码中有一个webservice调用,其中webmethod只是显示astring。我正在运行webservice作为localhost,但它不工作。我认为问题是从主线程本身调用webservice。请任何人都可以帮助从不同的线程调用它

SoapTestActivity.java

package com.sample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;



public class SoapTestActivity extends Activity {
    TextView result;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        result = (TextView)findViewById(R.id.result);

        final String NAMESPACE = "http://sample.com/";
        final String METHOD_NAME = "SayHello";    
        final String SOAP_ACTION = "http://sample.com/SayHello";
        final String URL = "http://192.168.1.104/HelloAndroid/Service1.asmx";

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);            
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            String resultValue = response.toString();

            result.setText(resultValue);           
        }
        catch (Exception e) {
            result.setText(e.getMessage());
        }
    }
}

Service1.asmx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

namespace HelloAndroid 
{
    [WebService(Namespace = "http://sample.com/")]   
    public class Service1 : System.Web.Services.WebService
    { [WebMethod] 
        public string SayHello() {
        return "Hello, Android from .NET";
    } 
    } 
}

2 个答案:

答案 0 :(得分:2)

建议您不要在应用程序的主线程中进行网络呼叫, 为此,您需要使用Async-task。 refer this

示例AsyncTask类

private class NetworkTask extends AsyncTask<Void, Void, Void> {

     final String NAMESPACE = "http://sample.com/";
        final String METHOD_NAME = "SayHello";    
        final String SOAP_ACTION = "http://sample.com/SayHello";
        final String URL = "http://192.168.1.104/HelloAndroid/Service1.asmx";
        String resultValue ="";
     protected Void doInBackground(Void... urls) {

         try {
             SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);            
             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
             envelope.dotNet = true;
             envelope.setOutputSoapObject(request);

             HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
             androidHttpTransport.call(SOAP_ACTION, envelope);
             SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
              resultValue = response.toString();


         }
         catch (Exception e) {
             resultValue = e.getMessage();
         }
     }

     protected void onPostExecute(Long result) {
         result.setText(resultValue);   
     }


 }

在onCreate中调用此asynctask as -

new NetworkTask().excecute();

答案 1 :(得分:0)

您可以使用async-task来执行此操作

class myAsyncTask extends AsyncTask<Void,Void,String>
{
protected void doInBackground(Void...params)
{
final String NAMESPACE = "http://sample.com/";
        final String METHOD_NAME = "SayHello";    
        final String SOAP_ACTION = "http://sample.com/SayHello";
        final String URL = "http://192.168.1.104/HelloAndroid/Service1.asmx";

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);            
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            String resultValue = response.toString();


        }
        catch (Exception e) {
            result.setText(e.getMessage());
        }
return resultValue;
}
protected void onPostExcecute(String result)
{
super.onPostExecute(result);
result.setText(result);   
}
}

现在通过在MainActivity中创建一个对象myasyncTask object =new myAsyncTask();来调用此异步任务 现在像object.execute();

那样执行它