如何在android中使用WCF服务?
我是android和wcf服务中的新成员。我已经创建了一个用于插入和托管的服务。我知道如何在Windows Phone中使用,但我不知道如何在Android中使用。如何在android中使用WCF服务?
这里的服务:http://wcfservice.triptitiwari.com/Service1.svc
请让我知道如何使用我的服务功能的Android?
看一看图书馆改造为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:改造:1.9.0'
compile'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile'com.squareup.okhttp: okhttp:2.0.0'
消耗WCF
服务,而无需使用任何网络库,例如Retrofit你将需要作为一个依赖添加ksoap2
您Gradle
项目。您也可以下载jar文件here
你必须的jar文件添加到libs文件夹在你的项目库目录/YourProject/app/libs/ksoap2.jar
然后还包括这一行你的应用程序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);
非常感谢你。让我试试这个 – user3086954 2015-03-03 11:10:50
让我知道它是否适合你 – Neil 2015-03-03 14:52:28
有没有简单的例子? – user3086954 2015-03-03 10:51:02
你没有得到什么,我会帮忙吗? – 2015-03-03 11:01:44
是的。实际上,在android中非常新,所以需要你的帮助来使用这个服务。我需要一步一步的过程来使用这个。我的Skype是msp.nitesh你能来吗? – user3086954 2015-03-03 11:09:11