我创建了一个HTTP客户端,该客户端将数据发送到我的服务器。此数据将查询我的服务器,该服务器将返回JSON对象。如何从服务器接收JSON对象响应并将其插入数据库?
下面的代码将向服务器发送 ContactID ,并且服务器将返回JSON对象如何从服务器获取JSON对象?我要添加什么代码?我已添加
var data = await response.Content.ReadAsStringAsync();
但我不知道如何进行。
try
{
var db = DependencyService.Get<ISQLiteDB>();
var conn = db.GetConnection();
var sql = "SELECT * FROM tblUser WHERE ContactID = '" + contact + "'";
var getUser = conn.QueryAsync<UserTable>(sql);
var resultCount = getUser.Result.Count;
//Check if the user has been sync
if (resultCount < 1)
{
try
{
syncStatus.Text = "Syncing user to server...";
var link = Constants.requestUrl + "Host=" + host + "&Database=" + database + "&Contact=" + contact + "&Request=8qApc8";
string contentType = "application/json";
JObject json = new JObject
{
{ "ContactID", contact }
};
HttpClient client = new HttpClient();
var response = await client.PostAsync(link, new StringContent(json.ToString(), Encoding.UTF8, contentType));
var data = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var userresult = JsonConvert.DeserializeObject<IList<UserData>>(content);
var count = userresult.Count;
for (int i = 0; i < count; i++)
{
try
{
syncStatus.Text = "Syncing user to server...";
var item = userresult[i];
var contactID = item.ContactID;
var userID = item.UserID;
var userPassword = item.UserPassword;
var userType = item.UserType;
var userStatus = item.UserStatus;
var lastSync = item.LastSync;
var serverUpdate = item.ServerUpdate;
var mobileUpdate = item.MobileUpdate;
var user = new UserTable
{
ContactID = Convert.ToInt32(contactID),
UserID = userID,
UserPassword = userPassword,
UserType = userType,
UserStatus = userStatus,
LastSync = lastSync,
ServerUpdate = serverUpdate,
MobileUpdate = mobileUpdate
};
await conn.InsertAsync(user);
}
catch (Exception ex)
{
Console.Write("Syncing user error " + ex.Message);
}
}
}
}
catch (Exception ex)
{
Console.Write("Syncing User Error " + ex.Message);
}
}
我的PHP代码将使用从 Xamarin HTTP Client 收到的 ContactID 查询我的数据库。
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$ContactID = $json_obj->ContactID;
$sql = "SELECT * FROM tblUser WHERE ContactID = '$ContactID'";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count > 0){
while ($row = @mysqli_fetch_array($result)) {
$decr = CryptRC4(FromHexDump($row['UserPassword']), $key);
$ar[] = array(
'ContactID' => $row['ContactID'],
'UserID' => $row['UserID'],
'UserPassword' => $decr,
'UserType' => $row['UserType'],
'UserStatus' => $row['UserStatus'],
'LastSync' => $sync,
'ServerUpdate' => $row['ServerUpdate'],
'MobileUpdate' => $row['MobileUpdate']
);
print json_encode($ar);
//Update LastSync DateTime
$sql = "UPDATE tblUser SET LastSync = '$sync' WHERE ContactID = '$ContactID'";
mysqli_query($conn, $sql);
}
}
答案 0 :(得分:3)
以上示例中的最后一条语句以字符串格式提供了json对象的列表。
var data = await response.Content.ReadAsStringAsync();
您需要将其转换回对象列表。为了让您的项目了解对象的定义,请创建一个具有公共属性的简单类(如下所示)
public class UserLog
{
public int ContactId { get; set; }
public string Log { get; set; }
public DateTime LogDate { get; set; }
}
将 Newtonsoft.Json (由James Newton-King编写)Nuget包添加到项目中,以便可以使用json。
要将变量“ data”的内容转换为UserLog对象的列表,请编写类似
的代码var list = NewtonsoftUtil<IList<UserLog>>.DeserializeObject(data);
(在文件顶部添加using Newtonsoft.Json;
)
请告诉我这是否有帮助。
答案 1 :(得分:1)
假设您已正确完成所有操作。换句话说,您可以发送您的 contactID 并获取一个json。
假设您的json结构类似于:
{"firstname" : "Doe",
"lastname" : "foo"
"age" : "27"}
检索数据的一种可能方法如下:
using Newtonsoft.Json;
//after PostAsync()
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
JObject jContent = (JObject)JsonConvert.DeserializeObject(content);
string firstName = (string)jContent.GetValue("firstname")
string lastName = (string)jContent.GetValue("lastname");
int age = (int)jContent.GetValue("age");
}
Newtonsoft在Nuget上可用。如果尚未安装,则需要安装。
如果您的json有许多如下所示的键/值对:
{ key1 : value1,
key2 : value2,
key3 : value3,
...
key10 : value10}
那么这样做并不是一个好主意:
string foo1 = (string)jContent.GetValue("key1");
string foo2 = (string)jContent.GetValue("key2");
//...
string foo10 = (string)jContent.GetValue("key10");
要处理这种情况,您可以创建一个类:
public class Foo
{
public string Foo1 {get;set;}
public string Foo2 {get;set;}
//...
public string Foo2 {get;set;}
}
然后,您可以执行以下简单操作:
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Foo foo = JsonConvert.DeserializeObject<Foo>(content);
}
从www.newtonsoft.com开始引用的改进的解决方案。转到那里,查看使用该库的其他方法。
答案 2 :(得分:1)
以上答案缺少一个要点-> efficiency
。
不需要在内存中分配字符串,尤其是在JSON大的情况下。 Streams
比string
更好:
// Read the response as stream
var stream = await response.Content.ReadAsStreamAsync();
// Use the next method for deserialization
T DeserializeJsonFromStream<T>(Stream stream)
{
if (stream == null || stream.CanRead == false)
return default(T);
using (var sr = new StreamReader(stream))
using (var jtr = new JsonTextReader(sr))
{
var js = new JsonSerializer();
return js.Deserialize<T>(jtr);
}
}