C# Volley 如何获取 OnResponse?
C# Volley how to get OnResponse?
请让我知道我需要在哪里使用 Volley 在 C# 中编写 "OnResponse"。
我将代码从 Android studio 转换为使用 Volley 库的 c#。然后我在 Xamarin 中打开代码发送请求并接收 response.I 添加了 Volley Nuget 包 (Xamarin.Bindings.Volley)。
以下是转换后的 C# 代码片段。但是,代码不识别 "OnResponse" 函数。我需要一个使用 Volley 在 c# 中成功实施 "OnResponse" 的示例。
public void GetAndPostReqquest(string url, int requestedCode, JSONObject @object, IServerResponse jsonResponse)
{
int method = Request.equestMethodConsts.Get;
switch (requestedCode)
{
case POST:
method = Request.equestMethodConsts.Post;
break;
case PUT:
method = Request.equestMethodConsts.Put;
break;
}
if (!IsInternetAvailable())
{
jsonResponse.RequestFinishedWithError(MContext.GetString(App8.Droid.Resource.String.internet_connection_is_not_available));
return;
}
jsonResponse.RequestStarted();
JsonObjectRequest request = new JsonObjectRequest(method, url, @object, Llistener, EerrorListener) {
private void OnResponse(JSONObject response)
{
IDictionary<string, string> headers = new Dictionary<string, string>();
string credentials = UNAME + ":" + PWD;
string auth = "Basic " + Convert.ToBase64String(credentials.ToCharArray(0, credentials.Length));
headers["Authorization"] = auth;
headers["Content-Type"] = "application/json; charset=utf-8";
//return headers;
}
}
要在您的代码中使用 Volley,您需要做一些小改动。
首先,我建议您将正在使用的 Nugget 包从 Xamarin.Bindings.Volley 更改为 Xamarin.Android.Volley.您当前使用的那个已经过时 (2017),而我建议的那个不仅是 up-to-date,而且还由 Xamarin 维护,所以它很可能会一直更新。
在执行代码的 Activity
中,您需要使其实现属于 Volley
命名空间的两个接口,它们是:Response.IListener
和 Response.IErrorListener
它应该是这样的:
public class MainActivity : Activity, Response.IListener, Response.IErrorListener
这将需要您通过在Activity中添加public void OnErrorResponse(VolleyError p0)
和public void OnResponse(Object p0)
两个方法来完成这些接口的实现class
在 OnResponse
方法中添加逻辑。
在使用之前,您需要将 OnResponse
中接收到的 Object
转换为 JSONObject
对象。
Activity
定义稍后应如下所示:
public class MainActivity : Activity, Response.IListener, Response.IErrorListener
{
public void OnErrorResponse(VolleyError p0)
{
}
public void OnResponse(Object p0)
{
if(p0 is JSONObject response)
{
// Do your logic here with response
}
}
/......
}
您的请求将更新为:
JsonObjectRequest request = new JsonObjectRequest(method, url, @object, this, this);
希望对您有所帮助。-
请让我知道我需要在哪里使用 Volley 在 C# 中编写 "OnResponse"。
我将代码从 Android studio 转换为使用 Volley 库的 c#。然后我在 Xamarin 中打开代码发送请求并接收 response.I 添加了 Volley Nuget 包 (Xamarin.Bindings.Volley)。 以下是转换后的 C# 代码片段。但是,代码不识别 "OnResponse" 函数。我需要一个使用 Volley 在 c# 中成功实施 "OnResponse" 的示例。
public void GetAndPostReqquest(string url, int requestedCode, JSONObject @object, IServerResponse jsonResponse)
{
int method = Request.equestMethodConsts.Get;
switch (requestedCode)
{
case POST:
method = Request.equestMethodConsts.Post;
break;
case PUT:
method = Request.equestMethodConsts.Put;
break;
}
if (!IsInternetAvailable())
{
jsonResponse.RequestFinishedWithError(MContext.GetString(App8.Droid.Resource.String.internet_connection_is_not_available));
return;
}
jsonResponse.RequestStarted();
JsonObjectRequest request = new JsonObjectRequest(method, url, @object, Llistener, EerrorListener) {
private void OnResponse(JSONObject response)
{
IDictionary<string, string> headers = new Dictionary<string, string>();
string credentials = UNAME + ":" + PWD;
string auth = "Basic " + Convert.ToBase64String(credentials.ToCharArray(0, credentials.Length));
headers["Authorization"] = auth;
headers["Content-Type"] = "application/json; charset=utf-8";
//return headers;
}
}
要在您的代码中使用 Volley,您需要做一些小改动。
首先,我建议您将正在使用的 Nugget 包从 Xamarin.Bindings.Volley 更改为 Xamarin.Android.Volley.您当前使用的那个已经过时 (2017),而我建议的那个不仅是 up-to-date,而且还由 Xamarin 维护,所以它很可能会一直更新。
在执行代码的 Activity
中,您需要使其实现属于 Volley
命名空间的两个接口,它们是:Response.IListener
和 Response.IErrorListener
它应该是这样的:
public class MainActivity : Activity, Response.IListener, Response.IErrorListener
这将需要您通过在Activity中添加public void OnErrorResponse(VolleyError p0)
和public void OnResponse(Object p0)
两个方法来完成这些接口的实现class
在 OnResponse
方法中添加逻辑。
在使用之前,您需要将 OnResponse
中接收到的 Object
转换为 JSONObject
对象。
Activity
定义稍后应如下所示:
public class MainActivity : Activity, Response.IListener, Response.IErrorListener
{
public void OnErrorResponse(VolleyError p0)
{
}
public void OnResponse(Object p0)
{
if(p0 is JSONObject response)
{
// Do your logic here with response
}
}
/......
}
您的请求将更新为:
JsonObjectRequest request = new JsonObjectRequest(method, url, @object, this, this);
希望对您有所帮助。-