11

次の形式で JSON 文字列を返す .NET フレームワークを使用して実装された SOAP Web サービス (.asmx) があります。

{"checkrecord":[{"rollno":"abc2","percentage":40,"attended":12,"missed":34}],"Table1":[]}

現在、私の Android アプリでは、ksoap を使用して次の方法で Web サービスを呼び出しています。

    public String getjsondata(String b)
{       

     String be=""; 

        SoapObject request = new SoapObject(namespace, method_NAME);      
        request.addProperty("rollno",b);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
        envelope.dotNet = true; 
        envelope.setOutputSoapObject(request);

        HttpTransportSE  android = new HttpTransportSE(url);

        android.debug = true; 

 try 
 {

    //android.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");   
    android.call(soap_ACTION, envelope);

    SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
    Log.i("myapp",result.toString());
    System.out.println(" --- response ---- " + result); 
    be=result.toString();

    if (be.startsWith("[")) 
    { // if JSON string is an array
        JSONArr = new JSONArray(be);

        System.out.println("length" + JSONArr.length());
        for (int i = 0; i < JSONArr.length(); i++) 
        {
            JSONObj = (JSONObject) JSONArr.get(i);
            bundleResult.putString(String.valueOf(i), JSONObj.toString());
            System.out.println("bundle result is"+bundleResult);
        } 

     }

   }
    catch (SocketException ex) { 
    ex.printStackTrace(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 


    return be;      


 }

応答を受け取りましたが、応答にレコードが含まれておらず、空白の値が表示されます。

Logcat からの応答は次のとおりです。

 11-21 20:13:03.283: INFO/myapp(1173): {"checkrecord":[],"Table1":[]}

 11-21 20:13:03.283: INFO/System.out(1173):  --- response ---- {"checkrecord":[],"Table1":[]}

なぜこれが起こっているのか誰にも教えてもらえますか?

私のWebサービスコード:

      using System;
      using System.Collections;
      using System.ComponentModel;
      using System.Data;
      using System.Linq;
      using System.Web;
      using System.Web.Services;
      using System.Web.Services.Protocols;
      using System.Xml.Linq;
      using System.Data.SqlClient;

namespace returnjson
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]

    public String getdata(String rollno)
    {
        String json;
        try
        {

            using (SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123"))
            {

           string select = "select * from checkrecord where rollno=\'" + rollno + "\'";
                SqlDataAdapter da = new SqlDataAdapter(select, myConnection);
                DataSet ds = new DataSet();
                da.Fill(ds, "checkrecord");
                DataTable dt = new DataTable();
                ds.Tables.Add(dt);
                json = Newtonsoft.Json.JsonConvert.SerializeObject(ds);

            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;

        }

        return json;

      }

    }
 }

編集: .NET でクライアント アプリを使用して Web サービス コードをテストしましたが、正常に動作しています。すべてのレコードと値を含む返された JSON 文字列に従って、適切な応答を取得しています。

4

2 に答える 2

4

この問題は、次のような特定のシンボルに関連しているようです : \ ソープ解析エラーが発生します。

Ethereal などのツールを使用して入力ストリームを分析し、正しく受信したことを確認できます。それが正しければ、jsonデータをエンコードしてから送信する必要があるかもしれません。

本体にJSONObjectを返すサーバー(ランプ)でテストするだけで、Androidは以下のコードを使用して正常に動作します:

public static void testApi() {
        AndroidHttpTransport androidHttptt = new AndroidHttpTransport("http://172.16.0.178/1mobile/market/services.php");
        SoapObject request = new SoapObject(NAMESPACE, "check_error");
        // request.addProperty("data", "empty");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        Element[] header = new Element[1];
        header[0] = new Element().createElement(NAMESPACE, "Credentials");
        envelope.dotNet = true;
        envelope.headerOut = header;
        envelope.setOutputSoapObject(request);

        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(url);
        androidHttpTransport.setXmlVersionTag(XML_HEAD);
        try {
            androidHttptt.call("http://172.16.0.178/check_error", envelope);
            SoapObject response = (SoapObject) envelope.getResponse();
            Log.e("response", "response=" + response.toString());
        } catch (Exception e) {

        }
    }

Etheral を使用すると、次のようなデータが得られます。

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:check_errorResponse xmlns:ns1="http://test.test.com">
<Result xsi:type="xsd:string">{&quot;checkrecord&quot;:[{&quot;rollno&quot;:&quot;abc2&quot;,&quot;percentage&quot;:40,&quot;attended&quot;:12,&quot;missed&quot;:34}],&quot;Table1&quot;:[]}</Result>
</ns1:check_errorResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

jsonデータが変換されていることがわかります

{&quot;checkrecord&quot;:[{&quot;rollno&quot;:&quot;abc2&quot;,&quot;percentage&quot;:40,&quot;attended&quot;:12,&quot;missed&quot;:34}],&quot;Table1&quot;:[]}
于 2011-11-24T03:58:08.157 に答える
-1

SOAP プロトコルは、XML を通信手段として使用します。リクエストは XML 文字列に変換され、サーバーからの応答は、KSOap が解析してオブジェクトを提供する XML 文字列になります。したがって、WebService からの応答は問題ないように見えますが、KSoap の解析に問題があります。皆さんが議論していたように、サーバーで文字列をエンコードして返すことができます。Android はすでに Base64 エンコード/デコードをサポートしています。

Web サービスでは、json の代わりに以下を返します。

Convert.ToBase64String (Encoding.UTF8.GetBytes (json));

これにより、エンコードされた文字列が返されます。したがって、取得する応答はエンコードされた文字列になります。したがって、Androidクライアント側で。

new String(android.util.Base64.decode(responseString.getBytes(),android.util.Base64.DEFAULT))

これにより、デコードされた文字列が得られます。

于 2011-11-28T12:23:11.373 に答える