【问题标题】:How to use WCF service in android?如何在android中使用WCF服务?
【发布时间】:2015-05-03 23:07:50
【问题描述】:

我是 android 和 wcf 服务的新手。我创建了一项插入服务并在此处托管。我知道如何在windows phone 中使用,但我不知道如何在android 中使用。

这里是服务:http://wcfservice.triptitiwari.com/Service1.svc

请告诉我如何在android中使用我的服务功能。?

【问题讨论】:

    标签: android wcf


    【解决方案1】:

    看看 android 的改造库:http://square.github.io/retrofit/

    它使用起来非常简单,而且非常可扩展。

    // below is the your client interface for wcf service 
    
    public interface IServer{
        @POST("/GetUserDetails")
        public void getUserDetails(@Body YourRequestClass request
    , Callback<YourResponseClass> response);
    
    }
    
    ...
    
    // code to below goes in a class
    
    IServer server;
    
    private Client newClient() {
            OkHttpClient okHttpClient = new OkHttpClient();
            okHttpClient.setSslSocketFactory(getSSLSocketFactory());
            return new OkClient(okHttpClient);
    }
    
    RestAdapter adapter = new RestAdapter.Builder()
                    .setConverter(new GsonConverter(gson))
                    .setLogLevel(APIUtils.getLogLevel())
                    .setClient(newClient())
                    .setEndpoint("wcf service url")
                    .build();
    this.server = adapter.create(IServer.class);
    
    
    
     ..
    

    使用示例一次全部设置

    server.getUserDetails( new YourRequestClass ,new Callback<YourResponseClass>() {
            @Override
            public void success(YourResponseClass yourResponse, Response response) {
                // do something on success
            }
    
            @Override
            public void failure(RetrofitError error) {
               // do something on error
            }
        });
    

    以下是您需要的库:

    编译'com.squareup.retrofit:retrofit:1.9.0'

    编译'com.squareup.okhttp:okhttp-urlconnection:2.0.0'

    编译'com.squareup.okhttp:okhttp:2.0.0'

    【讨论】:

    • 有什么简单的例子吗?
    • 你没有得到什么,我会帮忙?
    • 是的。实际上在 android 中非常新,所以需要您的帮助才能使用此服务。我需要一步一步的过程来使用它。我的 Skype 是 msp.nitesh 你能来吗?
    • 你能把你的 WCF 服务改成 JSON 吗?看看上面的代码,告诉我你是否不太明白,你使用的是android studio还是eclipse?
    • 如果您使用带有 JSON 和 Retrofit 的 WCF,那么理解上述内容将自然而然,顺便说一句,无需使用 Retrofit 进行序列化和反序列化,因为它已经为您完成了
    【解决方案2】:

    要使用WCF 服务而不使用Retrofit 等任何网络库,您需要将ksoap2 添加为Gradle 项目的依赖项。可以下载jar文件here

    您必须将 jar 文件添加到项目 libs 目录 /YourProject/app/libs/ksoap2.jar 中的 libs 文件夹中,然后还要将此行包含在您的应用程序 Gradle 文件中

    compile files('libs/ksoap2.jar')
    

    将其作为依赖项包含后,您将必须创建以下对象。它不必与我的实现完全一样,这只是它可能看起来的一个版本。


    YourWcfImplementation.java

    import android.os.AsyncTask;
    import android.support.v4.util.Pair;
    import android.util.Log;
    
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.PropertyInfo;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    
    import java.util.List;
    
    public class YourWcfImplementation {
        private static final String TAG = YourWcfImplementation.class.getSimpleName();
    
        private static final String NAMESPACE = "http://tempuri.org/";
        private static final String URL = "http://wcfservice.triptitiwari.com/Service1.svc";
        private static final String SERVICE_NAME = "IService1";
    
        private DataProcessingListener dataProcessingListener;
    
        public YourWcfImplementation(DataProcessingListener dataProcessingListener) {
            this.dataProcessingListener = dataProcessingListener;
        }
    
        /**
         * Invokes a server request with specified parameters
         * @param serviceTransportEntity
         */
        public void invokeServiceRequest(ServiceTransportEntity serviceTransportEntity) {
            new AsynchronousRequestTask().execute(serviceTransportEntity);
        }
    
        /**
         * Handles the request processing
         * @param params
         */
        private String processRequest(ServiceTransportEntity params) {
            String methodName = params.getMethodName();
    
            SoapObject request = new SoapObject(NAMESPACE, methodName);
            String soapAction = NAMESPACE + SERVICE_NAME + "/" + methodName;
    
            for (Pair<String, String> pair : params.getTransportProperties()) {
                PropertyInfo prop = new PropertyInfo();
                prop.setName(pair.first);
                prop.setValue(pair.second);
    
                request.addProperty(prop);
            }
    
            SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
    
            return executeHttpTransportCall(soapAction, envelope);
        }
    
        /**
         * Execute the http call to the server
         * @param soapAction
         * @param envelope
         * @return string response
         */
        private String executeHttpTransportCall(String soapAction, SoapSerializationEnvelope envelope) {
            String stringResponse;
    
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try {
                androidHttpTransport.call(soapAction, envelope);
    
                SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                stringResponse = String.valueOf(response);
            } catch (Exception e) {
                Log.e(TAG, "ERROR", e);
                stringResponse = e.getMessage();
            }
    
            return stringResponse;
        }
    
        /**
         * Builds the serialization envelope
         * @param request
         * @return SoapSerializationEnvelope
         */
        private SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
    
            return envelope;
        }
    
        /**
         * Handles asynchronous requests
         */
        private class AsynchronousRequestTask extends AsyncTask<ServiceTransportEntity, String, String> {
    
            @Override
            protected String doInBackground(ServiceTransportEntity... params) {
                return processRequest(params[0]);
            }
    
            @Override
            protected void onPostExecute(String response) {
                dataProcessingListener.hasProcessedData(response);
            }
        }
    
        public interface DataProcessingListener {
            public void hasProcessedData(String data);
        }
    }
    

    ServiceTransportEntity.java

    /**
     * Entity that holds data used in the soap request
     */
    public class ServiceTransportEntity {
        private String methodName;
        private List<Pair<String, String>> transportProperties;
    
        public ServiceTransportEntity(String methodName, List<Pair<String, String>> transportProperties) {
            this.methodName = methodName;
            this.transportProperties = transportProperties;
        }
    
        public String getMethodName() {
            return methodName;
        }
    
        public List<Pair<String, String>> getTransportProperties() {
            return transportProperties;
        }
    }
    

    然后您将使用类似于此的代码实现该类

    List<Pair<String, String>> properties = new ArrayList<>();
    properties.add(new Pair<>("PropertyName", "PropertyValue"));
    
    ServiceTransportEntity serviceTransportEntity = new ServiceTransportEntity("SomeMethodName", properties);
    YourWcfImplementation wcfImplementation = new YourWcfImplementation(new YourWcfImplementation.DataProcessingListener() {
        @Override
        public void hasProcessedData(String response) {
            //Do something with the response
        }
    }).invokeServiceRequest(serviceTransportEntity);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 2012-09-26
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多