android - 在 SQL Server 中存储 JSONArray 的 Web 服务

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

我有一个 Android 应用程序正在创建一个 JSONArray:

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;
   }

然后我尝试调用看起来像的网络服务 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

最佳答案

您将 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);

关于android - 在 SQL Server 中存储 JSONArray 的 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10272865/

相关文章:

c# - 获取写入文件时字符串占用的大小(以字节为单位)?

.net - TextRenderer.MeasureText 返回字符 "&"的 Width=0

android - 处理程序后执行队列的顺序不正确

android - appcompat-v7 v21 找不到类

android - 将 android drawable 添加到菜单 XML 停止应用程序编译

sql - 使用 BULK INSERT 映射列

java - Mockito 验证在第二个单元测试中失败

sql - 查找所有组合

sql - SQL Server 中是否有相当于 python 的 SequenceMatcher 来连接相似的列?

mysql - 为什么我的 MySQL 事务无法从 VB 运行?