Android 从SOAP web服务返回的JSON字符串不包含表的任何记录
我使用.NET framework实现了一个SOAP web service.asmx,它以以下形式返回JSON字符串: {checkrecord:[{rollno:abc2,百分比:40,参加人数:12,未参加人数:34}],表1:[] 现在,在我的Android应用程序中,我使用ksoap以以下方式调用web服务:Android 从SOAP web服务返回的JSON字符串不包含表的任何记录,android,json,web-services,android-ksoap2,Android,Json,Web Services,Android Ksoap2,我使用.NET framework实现了一个SOAP web service.asmx,它以以下形式返回JSON字符串: {checkrecord:[{rollno:abc2,百分比:40,参加人数:12,未参加人数:34}],表1:[] 现在,在我的Android应用程序中,我使用ksoap以以下方式调用web服务: public String getjsondata(String b) { String be=""; SoapObje
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;
}
谁能告诉我为什么会这样
我的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字符串以及所有记录和值获得正确的响应。似乎此问题与某些特定符号有关,如:\lead to soap parse error 您可以使用诸如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">{"checkrecord":[{"rollno":"abc2","percentage":40,"attended":12,"missed":34}],"Table1":[]}</Result>
</ns1:check_errorResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP协议使用XML作为通信手段。您的请求将转换为XML字符串,来自服务器的响应将是一个XML字符串,KSOap将解析该字符串并为您提供一个对象。 因此,来自WebService的响应看起来不错,但KSoap解析存在问题。所以,正如你们所讨论的,你们可以在服务器中对字符串进行编码并返回它。 Android已经提供了对Base64编码/解码的支持 在webservice中,返回以下内容,而不是json
Convert.ToBase64String (Encoding.UTF8.GetBytes (json));
这将返回一个编码字符串。因此,您将得到一个编码字符串。所以在android客户端
new String(android.util.Base64.decode(responseString.getBytes(),android.util.Base64.DEFAULT))
这将为您提供解码的字符串。希望它有帮助是的,根据您的描述,我认为soap不能正常解析json数据OK,但您能给我一个soap web服务编码json并返回json字符串的示例吗?我已经在我的帖子上发布了我的web服务代码。如果要进行编码,您可以建议对其进行修改吗?@notenking如果您真的使用Ethereal,您可能希望升级。早在2006年,我们就将该项目更名为Wireshark@notenking:我从web服务返回的字符串如前所述。我不明白它为什么显示空记录。您说过可能会出现/parse错误,但如果是这样,我不会得到任何这样的响应,对吗?JObject o=new JObject.parsejson->这能帮助我将JSON字符串转换成可以返回到Android应用程序的JSONObject吗?你确定服务器的答案是该字符串吗?@njzk2:是的,当然没有疑问:-好吧,如果web服务正在为另一个客户端工作,请求或响应逻辑不正确-请使用TCPMON比较工作和非工作客户端,并发布结果。我尝试过,但仍然没有使用相同的问题?顺便问一下,解码后的字符串是什么格式?是否为base64?请在Web服务中再次检查它是否正确返回。解码后,它将是JSON字符串。您已在服务器端将JSon字符串编码为Base64字符串,并对其进行解码以获得JSon字符串这是我在解码前后在logcat中得到的:…web服务工作正常..它在服务器端正确返回编码字符串..但服务器端编码字符串和客户端响应字符串不匹配,因为响应此处包含空记录名称:SoapSerializationEnvelope=new SoapSerializationEnvelope SoapEnvelope.VER11;envelope.setOutputSoapObjectrequest;HttpTransportSE httpTransport=新的HttpTransportsSoapur;httpTransport.callsoapAction,信封;对象resultsRequestSOAP=envelope.getResponse;试试这个,我用它来调用Web服务。我得到了正确的字符串
Convert.ToBase64String (Encoding.UTF8.GetBytes (json));
new String(android.util.Base64.decode(responseString.getBytes(),android.util.Base64.DEFAULT))