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">{"checkrecord":[{"rollno":"abc2","percentage":40,"attended":12,"missed":34}],"Table1":[]}</Result>
</ns1:check_errorResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
You can find the json data have been convered to
{"checkrecord":[{"rollno":"abc2","percentage":40,"attended":12,"missed":34}],"Table1":[]}