JSON String returned from SOAP web service containing no records for table

人走茶凉 提交于 2019-12-30 17:29:09

问题


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.


回答1:


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;:[]}



回答2:


SOAP protocol uses XML as means of communication. Your Request will be converted to a XML String and the response from the server will be a XML String which the KSOap parses and gives you an Object. So it looks like the response from WebService is fine but there is a problem with KSoap parsing. So as you guys were discussing you can encode the String in the server and return it. Android already provides support for Base64 encoding/decoding.

In the webservice return the following instead of json.

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

This will return you an encoded string. So the response you will get be an encoded string. So in the android client side.

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

which will give u the decoded string.Hope it helps



来源:https://stackoverflow.com/questions/8213654/json-string-returned-from-soap-web-service-containing-no-records-for-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!