有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

需要一些将Java代码Android Studio移植到C#Visual Studio的帮助吗

我需要一些帮助来将一段Java代码移植到C#。代码是关于对web api的post请求,以建立我在Java中使用的Volley连接,并且我已经在Visual Studio中安装了NuGet。我无法将StringRequest函数转换为C#

这是我目前试图在C#中移植的一段代码,但在C#中声明参数时出错。例如,它无法识别请求。方法和新的响应。听众

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response)
            {
                Log.i("Volley Response", response);
            }
        }, new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                Log.e("Volley Error", error.toString());
            }
        })

        {
            @Override
            public String getBodyContentType()
            {
                return "application/json; charset=utf-8";
            }

            @Override
            public byte[] getBody()
            {
                try
                {
                    return requestBody.getBytes("utf-8");
                }
                catch (UnsupportedEncodingException uee)
                {
                    return null;
                }
            }
        };
        requestQueue.add(stringRequest);
        return stringRequest;

如果有人能这么好地帮我把它移植到C#,我会非常高兴


共 (1) 个答案

  1. # 1 楼答案

    我自己解决了这个问题,没有使用截击,也没有移植java代码

    再次抱歉要求移植,我不知道这不是我应该在这里要求的。感谢jrswgtr让我知道

    这是一段代码,用于向web api发出简单的POST请求,从两个editbox获取值,并在单击按钮后发送它们

    public class MainActivity : AppCompatActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.activity_main);
                Button invia = FindViewById<Button>(Resource.Id.submit);
                EditText utenteInserito = FindViewById<EditText>(Resource.Id.utente);
                EditText passwordInserito = FindViewById<EditText>(Resource.Id.password);
    
                invia.Click += async delegate
                {
                    HttpClient client = new HttpClient();
                    string utente = utenteInserito.Text;
                    string password = passwordInserito.Text;
                    string json = "{'user': '"+utente+"','password': '"+password+"'}";
                    string URL = "http://192.168.1.11:57279/api/utente";
                    var content = new StringContent(json, Encoding.UTF8, "application/json");
                    var response = await client.PostAsync(URL, content);
                    var responseString = await response.Content.ReadAsStringAsync();
                };
            }
        }
    

    使用定义的模型更新代码

    invia.Click += delegate
                {
                    string user = utenteInserito.Text;
                    string password = passwordInserito.Text;
                    HttpClient client = new HttpClient();
                    client.BaseAddress = new Uri("http://192.168.1.11:57279/api/");
                    var utente = new Utente
                    {
                        user = user,
                        password = password
                    };
                    var postTask = client.PostAsJsonAsync<Utente>("utente", utente);
                    postTask.Wait();
                };