【发布时间】:2011-11-21 14:46:14
【问题描述】:
我有一个使用 .NET 框架实现的 SOAP Web 服务 (.asmx),它以这种形式返回一个 JSON 字符串:
{"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":[]}
谁能告诉我为什么会这样?
我的网络服务代码:
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 字符串以及所有记录和值获得正确的响应。
【问题讨论】:
-
你确定服务器的答案是那个字符串吗?
-
@njzk2 : 是的,当然没有疑问 :-)
-
好吧,如果 Web 服务正在为另一个客户端工作,则请求或响应逻辑不正确 - 使用 TCPMON 比较工作和非工作客户端,并发布您的结果。
标签: android json web-services android-ksoap2