需要一些帮助将Java代码Android Studio移植到C#Visual Studio [关闭]

问题描述 投票:-4回答:1

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

这是我目前正试图在C#中移植的代码片段,但在C#中声明params时出错。例如,它无法识别Request.Method和新的Response.Listener。

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#那么我会非常高兴。

java c# porting
1个回答
0
投票

我设法自己修复它而不使用Volley并且没有移植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();
            };
© www.soinside.com 2019 - 2024. All rights reserved.