尝试将Android App连接到.NET“.svc”Web服务和服务器响应不支持媒体类型

时间:2011-06-02 13:53:28

标签: .net android web-services

我正在尝试将Android App连接到具有.svc扩展名的Axapta网络服务。 我正在使用Ksoap 2.5.4 jar和所有依赖项。以下是代码。 我无法连接到Web服务,因为响应转储表明服务器不支持该媒体类型。


网络服务:

从Windows Phone连接时正常运行


代码:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.ServiceConnection;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class SoapActivity extends Activity {
     private static String mSOAPACTION="http://schemas.microsoft.com/dynamics/2008/01/services/LocalEndpointService/read",
                           mOPERATIONNAME="read",
                           WSDL_TARGET_NAMESPACE = "http://schemas.microsoft.com/dynamics/2008/01/services/";

     private static String mNAMESPACE="http://schemas.microsoft.com/dynamics/2008/01/services/";
     private static String mMETHODNAME="read";
     private static String mURL="http://192.168.100.188/MicrosoftDynamicsAXAif50/localendpointservice.svc?wsdl";
     private SoapPrimitive result = null;
     public static Context context=null;
     private HttpTransportSE  httpTransportSE = null;
     private TextView tv = null;

     public void onCreate(Bundle savedInstanceState){
     SoapSerializationEnvelope envelope = null;
     SoapObject Request = null;
     ServiceConnection sc = null;
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     tv = (TextView)findViewById(R.id.tv);
     try {
         envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
         envelope.setAddAdornments(false);
         envelope.encodingStyle = SoapSerializationEnvelope.ENC;
         envelope.env=SoapSerializationEnvelope.ENV;
         envelope.implicitTypes = true;
         Request = new SoapObject(mNAMESPACE, mMETHODNAME);
         //Request = new SoapObject(mNAMESPACE, mOPERATIONNAME);         
         //Request.addProperty("LocalEndpointServiceReadRequest", "Qing");       
         envelope.dotNet = true;
         envelope.setOutputSoapObject(Request);      
         //envelope.addMapping(mNAMESPACE, "UserCredentials",new UserCredendtials().getClass());
         /*androidHttpTransport = new AndroidHttpTransport(mURL);
         androidHttpTransport.debug = true;
         androidHttpTransport.call(mSOAPACTION, envelope);*/

         httpTransportSE = new HttpTransportSE(mURL);
         /*sc = httpTransportSE.getConnection();
         if(sc!=null){
             sc.setRequestProperty("Content-Type", "text/xml");
         }else{
             Log.i(getClass().getSimpleName(),"SC : null.");
         }*/

         httpTransportSE.debug = true;
         //httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
         httpTransportSE.call(mSOAPACTION, envelope);
         Object response = envelope.getResponse();
         tv.setText(response.toString());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
             Log.i(getClass().getSimpleName(),"requestDump : "+httpTransportSE.requestDump);
             Log.i(getClass().getSimpleName(),"responseDump : "+httpTransportSE.responseDump);
        }
    }
}

requestDump

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:d="http://www.w3.org/2001/XMLSchema" 
            xmlns:c="http://www.w3.org/2001/12/soap-encoding" 
            xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
  <v:Header />
  <v:Body>
    <read xmlns="http://schemas.microsoft.com/dynamics/2008/01/services/" />
  </v:Body>
</v:Envelope>

responseDump

  

服务器无法为请求提供服务,因为媒体类型不受支持。

注意:我已更新代码,因为我发现Web服务属于Soap版本1.2。

1 个答案:

答案 0 :(得分:1)

我认为目前的问题出现在网址中。该服务的URL不是:

http://192.168.100.188/MicrosoftDynamicsAXAif50/localendpointservice.svc?wsdl

这是服务描述的URL(WSDL文件),它只接受HTTP GET请求(对WCF服务的所有SOAP请求都是HTTP POST)。您的评论请求显示您正在尝试向此URL发送请求 - 它将始终失败。该服务的URL没有?wsdl查询字符串:

http://192.168.100.188/MicrosoftDynamicsAXAif50/localendpointservice.svc