将结果分配给 json 但是如果我使用 Debug.WriteLine(GETUserList()); 它打印给我;但我尝试分配 json 并获取 json 的值它返回 null
String json = GETUserList();
namespace PhoneApp3
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
const String url = "http://hackathon.kimhieu.info/flashcard/index.php/api/";
const String ContentType = "application/x-www-form-urlencoded";
public String username;
public String password;
public MainPage()
{
InitializeComponent();
username = "a";
password = "a";
String json = GETUserList();
MessageBox.Show(json);
}
GET user;
public String GETUserList()
{
user = new GET();
user.urlAdress = url + "user";
user.username = username;
user.password = password;
user.Completed += OutUser;
user.sendGETRequest();
return user.result;
}
// OUT GET
void OutUser()
{
Debug.WriteLine(user.result);
}
}
}
GET 类提供从服务器获取数据的功能
public class GET
{
public static event Action Completed;
public static String result = String.Empty;
public static String username;
public static String password;
public static string myRequestData = string.Empty;
public static String urlAdress;
public static void sendGETRequest()
{
System.Uri targetUri = new System.Uri(urlAdress);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
request.Method = "GET";
request.Headers["X_USERNAME"] = username;
request.Headers["X_PASSWORD"] = password;
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
}
private static void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
result = httpwebStreamReader.ReadToEnd();
}
myResponse.Close();
if (Completed != null)
Completed();
}
}
因为我在进程中看到它在调用一段时间后返回字符串。如何将结果分配给变量。感谢提前
如果我打电话
Debug.WriteLine("Hellllllllllll" + json + "HHHHHHHHHHHHHH");
JsonParsing jsonParsing = new JsonParsing(json);
Debug.WriteLine("Hllllllllllll" + json + "HHHHHHHHHHHHHH");
jsonParsing.GetListCardSet();
结果是
HllllllllllllHHHHHHHHHHHHHH
HllllllllllllHHHHHHHHHHHHHH
[{"id":"3","username":"a","password":"a"},{"id":"4","username":"bbbb\n","password":"aaa"},{"id":"5","username":"ab","password":"ab"},{"id":"6","username":"abc","password":"abc"},{"id":"7","username":"abc","password":"abc"},{"id":"8","username":"abcd","password":"abcd"},{"id":"9","username":"abcde","password":"abcde"},{"id":"10","username":"abcdef","password":"abcdef"},{"id":"11","username":"abcdef","password":"abcdef"},{"id":"12","username":"abcdef","password":"abcdef"},{"id":"13","username":"abcdef","password":"abcdef"},{"id":"14","username":"abcdef","password":"abcdef"},{"id":"15","username":"abcdef","password":"abcdef"},{"id":"16","username":"abcdef","password":"abcdef"},{"id":"17","username":"abcdef",