我正在通过 JSON 将包含两个节点“消息”和“发件人”的列表从 webService 发送到 android 设备,现在我想将发件人和消息分开,然后在 TextView 中显示它们
网络服务
[WebMethod]
public string GetMessage(String receiver)
{
try
{
con.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = con;
myCommand.CommandText = "SELECT message, sender FROM [Messages] WHERE receiver = '" + receiver + "'";
SqlDataReader reader;
reader = myCommand.ExecuteReader();
List<string> message = new List<string>();
if (reader.HasRows)
{
while (reader.Read())
{
message.Add (Convert.ToString(reader["sender"]));
message.Add (Convert.ToString(reader["message"]));
}
}
else
{
return USER_NOT_EXIST_CODE;
}
var json = new JavaScriptSerializer().Serialize(message);
return json.ToString();
}
主要活动
protected void onPostExecute(String params) {
super.onPostExecute(params);
progressDialog.dismiss();
try {
Log.v("XXX", params);
JSONObject jsonObj = new JSONObject(params);
message.setText(jsonObj.getString("message"));
sender.setText(jsonObj.getString("sender"));
} catch (JSONException e) {
e.printStackTrace();
}
}
当前的 LogCat
["0333 ","hi jigZ","0333 ","i am fine","0333 ","hi there"]