JSON字符串从包含任何记录的表SOAP Web服务返回 [英] JSON String returned from SOAP web service containing no records for table

查看:205
本文介绍了JSON字符串从包含任何记录的表SOAP Web服务返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SOAP Web服务(的.asmx)使用.NET框架返回我一个JSON字符串以这种形式实现的:

  

  {checkrecord:[{rollno:ABC2,百分比:40,出席:12,遗漏:34}],表1:[]}   

现在在我的Andr​​oid应用我使用KSOAP调用以下列方式Web服务:

 公共字符串getjsondata(字符串B)
{

     字符串中=;

        SoapObject请求=新SoapObject(命名空间,METHOD_NAME);
        request.addProperty(rollno,B);

        SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = TRUE;
        envelope.setOutputSoapObject(要求);

        HttpTransportSE机器人=新HttpTransportSE(URL);

        android.debug = TRUE;

 尝试
 {

    //android.setXmlVersionTag("<?xml版本= \1.0 \编码= \UTF-8 \&GT;?);
    android.call(soap_ACTION,包);

    SoapPrimitive结果=(SoapPrimitive)envelope.getResponse();
    Log.i(MyApp的,result.toString());
    的System.out.println( - 回应 - +结果);
    是= result.toString();

    如果(be.startsWith([))
    {//如果JSON字符串数组
        JSONArr =新JSONArray(定);

        的System.out.println(长度+ JSONArr.length());
        的for(int i = 0; I&LT; JSONArr.length();我++)
        {
            JSONObj =(的JSONObject)JSONArr.get(我);
            bundleResult.putString(将String.valueOf(ⅰ),JSONObj.toString());
            的System.out.println(捆绑的结果是+ bundleResult);
        }

     }

   }
    赶上(SocketException前){
    ex.printStackTrace();
    }赶上(例外五){
    e.printStackTrace();
    }


    回报是;


 }
 

我得到一个响应,但响应不包含任何记录并显示空值。

下面是从logcat的响应:

  11-21 20:13:03.283:信息/ MyApp的(1173):{checkrecord:[],表1:[]}

 11-21 20:13:03.283:信息/的System.out(1173):---响应---- {checkrecord:[],表1:[]}
 

谁能告诉我,为什么会出现这种情况?

我的网络服务code:

 使用系统;
      System.Collections中使用;
      使用System.ComponentModel;
      使用System.Data这;
      使用System.Linq的;
      使用的System.Web;
      使用System.Web.Services;
      使用System.Web.Services.Protocols;
      使用System.Xml.Linq的;
      使用System.Data.SqlClient的;

命名空间returnjson
{
///&LT;总结&gt;
///摘要说明服务1
///&LT; /总结&gt;
[WebService的(命名空间=htt​​p://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
[的ToolboxItem(假)]
//要允许此Web服务从脚本调用,使用ASP.NET AJAX,取消注释以下行。
// [System.Web.Script.Services.ScriptService]
公共类服务1:System.Web.Services.WebService
{
    [WebMethod的]

    公共字符串的getData(字符串rollno)
    {
        JSON字符串;
        尝试
        {

            使用(SqlConnection的MyConnection的=新的SqlConnection(@数据源= \ SQLEX $ P $干燥综合征;初始目录=学生;用户ID = SA;密码= 123))
            {

           串选择=SELECT * FROM checkrecord那里rollno = \'+ rollno +\';
                SqlDataAdapter的DA =新的SqlDataAdapter(选择,MyConnection的);
                的DataSet ds为新的DataSet();
                da.Fill(DS,checkrecord);
                数据表DT =新的DataTable();
                ds.Tables.Add(DT);
                JSON = Newtonsoft.Json.JsonConvert.SerializeObject(DS);

            }
        }

        赶上(例外前)
        {
            Console.WriteLine(ex.Message);
            返回null;

        }

        返回JSON;

      }

    }
 }
 

编辑:我已经使用的.NET客户端应用程序测试我的web服务code和它工作fine..getting一个适当的响应,每个返回的JSON字符串的所有记录和值

解决方案

这似乎这个问题涉及到像一些特定符号:\导致肥皂解析错误

您可以像使用抓包工具的一些分析仪的输入流,并确保你已经正确接收。如果它是正确的,可能是你应该EN code,你的JSON数据,然后将其发送。

我只是测试它在我的服务器(灯),它在体内返回的JSONObject,并低于code Android的使用,正常工作

 公共静态无效testApi(){
        AndroidHttpTransport androidHttptt =新AndroidHttpTransport(http://172.16.0.178/1mobile/market/services.php);
        SoapObject请求=新SoapObject(命名空间check_error);
        // request.addProperty(数据,空);
        SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);
        元素[]头=新的元素[1];
        标题[0] =新元素()的createElement(命名空间中,资格证书)。
        envelope.dotNet = TRUE;
        envelope.headerOut =报头;
        envelope.setOutputSoapObject(要求);

        AndroidHttpTransport androidHttpTransport =新AndroidHttpTransport(URL);
        androidHttpTransport.setXmlVersionTag(XML_HEAD);
        尝试 {
            androidHttptt.call(http://172.16.0.178/check_error,信封);
            SoapObject响应=(SoapObject)envelope.getResponse();
            Log.e(响应,响应=+ response.toString());
        }赶上(例外五){

        }
    }
 

与醚类,我得到的数据是这样的:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; SOAP-ENV:信封SOAP-ENV:encodingStyle的=htt​​p://schemas.xmlsoap.org/soap/encoding/的xmlns:SOAP-ENV =htt​​p://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/">
&LT; SOAP-ENV:身体与GT;
&LT; NS1:check_errorResponse的xmlns:NS1 =htt​​p://test.test.com&GT;
&LT;结果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>
&LT; / NS1:check_errorResponse&GT;
&LT; / SOAP-ENV:身体与GT;
&LT; / SOAP-ENV:信封&GT;
 

您可以找到JSON数据已经convered到

<$p$p><$c$c>{&quot;checkrecord&quot;:[{&quot;rollno&quot;:&quot;abc2&quot;,&quot;percentage&quot;:40,&quot;attended&quot;:12,&quot;missed&quot;:34}],&quot;Table1&quot;:[]}

I have a SOAP web service(.asmx) implemented using the .NET framework that returns me a JSON String in this form:

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

Now in my Android app I am using ksoap to call the web service in the following way:

    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;      


 }

I am getting a response but the response doesn't contain any records and shows blank values.

Here is response from 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":[]}

Can anyone tell me why is this happening?

My web service code :

      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;

      }

    }
 }

Edit: I have tested my web service code using a client app on .NET and it is working fine..getting a proper response as per returned JSON String with all records and values.

解决方案

It's seem this problem relate to some specific symbol like : \ lead to soap parse error.

you can use some tool like Ethereal to analyzer the input stream and make sure you have received correctly. if it's right, may be you should encode you json data and then send it.

I just test it on my server(lamp)which return a JSONObject in body,and android use below code,work normally:

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) {

        }
    }

with Etheral,I get the data like this:

<?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>

You can find the json data have been convered to

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

这篇关于JSON字符串从包含任何记录的表SOAP Web服务返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆