在SQL Server中存储JSONArray的Web服务

时间:2012-04-22 22:55:48

标签: android sql-server vb.net web-services arrays

我有一个用于创建JSONArray的Android应用程序:

List<String[]> sqlist = MySQLiteHelper.selectAll();
JSONArray jsArray = new JSONArray(sqlist);

正在调用

public static List<String[]> selectAll()
    {
        List<String[]> list = new ArrayList<String[]>();
        Cursor cursor = db.query(TABLE_BREADCRUMBS, new String[] 
                { "id","tim","lat","lon"}, null, null, null, null, null); 
       int x=0;
        if (cursor.moveToFirst()) {
           do {
                String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),
cursor.getString(3)};
                list.add(b1);
                x=x+1;
           } while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) {
           cursor.close();
        } 
       cursor.close();
        return list;
   }

然后我试图调用看起来像

的Web服务BoldApi
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data.SqlClient


' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class BoldApi
    Inherits System.Web.Services.WebService

    Dim b_id As Integer
    Dim b_time As String
    Dim b_lat As String
    Dim b_lon As String

    Public Property idBreadcrumb() As Integer
        Get
            Return b_id
        End Get
        Set(ByVal value As Integer)
            b_id = value
        End Set
    End Property

    Public Property timeBreadcrumb() As String
        Get
            Return b_time
        End Get
        Set(ByVal value As String)
            b_time = value
        End Set
    End Property

    Public Property latBreadcrumb() As String
        Get
            Return b_lat
        End Get
        Set(ByVal value As String)
            b_lat = value
        End Set
    End Property

    Public Property lonBreadcrumb() As String
        Get
            Return b_lon
        End Get
        Set(ByVal value As String)
            b_lon = value
        End Set
    End Property


    <WebMethod()> _
    Public Function JSONpass(ByVal jsArray() As BoldApi) As Boolean
        For X = 0 To jsArray.Length - 1
            ' Usage example
            b_id = jsArray(X).idBreadcrumb()
            b_time = jsArray(X).timeBreadcrumb()
            b_lat = jsArray(X).latBreadcrumb()
            b_lon = jsArray(X).lonBreadcrumb()

            InsertRow(b_time, b_lat, b_lon)

        Next
        Return True
    End Function

    Public Sub InsertRow(time As String, lat As String, lon As String)
        Dim myConnectionString As String = "Data Source=tcp:sql2k804.discountasp.net;Initial Catalog=SQL2008R2_868128_mjk;User ID=********;Password=******;"
        Dim myConnection As New SqlConnection(myConnectionString)
        Dim myInsertQuery As String = "INSERT INTO dbo.bold_breadcrumbs (latitude, longitude) values (lat,lon)"
        Dim myCommand As New SqlCommand(myInsertQuery)
        myCommand.Connection = myConnection
        myConnection.Open()
        myCommand.ExecuteNonQuery()
        myCommand.Connection.Close()
    End Sub 'SelectSqlClientSrvRows


End Class

使用看起来像

的HttpPost
HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.gamoda.org/r/Bold/api/BoldApi.asmx");


                try {
                    httppost.setEntity((HttpEntity) jsArray);
                    HttpResponse response = httpclient.execute(httppost);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

我收到错误: java.lang.ClassCastException:org.json.JSONArray

1 个答案:

答案 0 :(得分:1)

将jsArray定义为JSONArray对象,JSONArray jsArray = new JSONArray(sqlist); 但是,您发送http请求强制的代码会将其强制转换为HttpEntity,这会导致无效并抛出java.lang.ClassCastException: org.json.JSONArray。你应该自己将jsArray转换为实体对象。

Map<String, String> param = .... //here is a Map object for example, edit to make it fit your case
ArrayList<BasicNameValuePair> postData = new ArrayList<BasicNameValuePair>();
for (Map.Entry<String, String> m : param.entrySet()) {
    postData.add(new BasicNameValuePair(m.getKey(), m.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postData);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);