【问题标题】:How to create JSONArray in c# and send that data to Android app如何在 C# 中创建 JSONArray 并将该数据发送到 Android 应用程序
【发布时间】:2014-09-24 08:13:59
【问题描述】:

在我的 android 应用程序中,我的数据库位于 MS SQL 服务器上。为了访问该数据库,我正在创建一个 c# 脚本并从与 php 相同的应用程序中调用。我的活动是一个登录活动,用户输入他的用户名和密码,然后单击按钮,该 c# 脚本正在调用以检查该用户名和密码是否有效。如果有效,则将以 JSONArray 格式发送该用户的数据,如果无效,则将以 json 格式发送code=0。但我不知道如何将 JSON 数据从 c# 发送到 android 之类的 php。这是我写的任何内容下面的c#代码。请帮助我如何将 JSON 数据从 MS SQL 和 c# 发送到 Android。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LoginForm
{
    public class LoginCheck
    {
        string strUser=Request.Querystring["strUser"];//getting username from activity
        string strPassword=Request.Querystring["strPassword"];//getting password from activity

        try
        {
            SqlDataReader myReader = null;
            SqlConnection conn = new SqlConnection("Data Source=***.**.**.*; Initial Catalog=GPSDB;Persist Security Info=True;User ID=##;Password=##$$#@!@!#");
                    conn.Open();
            SqlCommand check = new SqlCommand("select * from Table_User where strUser=@strUser AND strPassword=@strPassword;", conn);
            check.Parameters.AddWithValue("@strUser",strUser);
            check.Parameters.AddWithValue("@strPassword",strPassword);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if(dt.Rows.Count>0)
            {
                //JSON code...
            }    
        }
    }
}

【问题讨论】:

  • 看看JavascriptSerilizer Class MSDN 并且已经有很多答案如果你搜索你会得到它就像here 是一个。试试看,如果您有任何问题,请告诉我们
  • 但我想将此数据发送到 android 应用程序。我只使用 c# 文件而不是 .aspx 文件。
  • 你是什么意思你只使用 C# 文件。是网络服务吗?
  • 是的,它是一个类似 php 的网络服务..!!!
  • 那么您将使其成为RESTFul 服务我假设如果是这种情况那么简单您必须定义方法的属性以使其平静Here 是链接。然后你需要序列化数据并返回它。检查这些答案以供参考 12

标签: c# android json database arrays


【解决方案1】:

您发布的代码甚至在语法上都不正确。您可能需要学习更多 C#。只是说说而已。

对于您的服务,您需要一些您可以实际托管的东西。今天要走的路是使用 ASP.NET MVC 4 Web api 控制器实现的 REST 服务。见http://www.asp.net/web-api

这样您就可以简单地将数据放入强类型 DTO 中并返回它们。然后,Web Api 将根据请求标头为您序列化它们,例如XML 或 JSON。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-13
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多