需要一些帮助将 Java 代码 Android Studio 移植到 C# Visual Studio
Need some help porting Java code Android Studio to C# Visual Studio
我需要一些帮助将一段 Java 代码移植到 C#。该代码是关于 post 对网络 api 的请求,以建立连接 我在 Java 中使用了 Volley,并且我已经在 Visual Studio 中安装了 NuGet。我在将 StringRequest 函数转换为 C# 时遇到问题。
这是我目前尝试在 C# 中移植的一段代码,但在 C# 中声明参数时出现错误。例如,它无法识别 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# 我会很高兴。
我在不使用 Volley 和移植 java 代码的情况下自行修复了它。
再次抱歉请求移植,我不知道这不是我应该在这里问的问题。感谢 jrswgtr 让我知道。
这是向网络 api 发出简单 POST 请求的代码,从两个编辑框中获取值并在单击按钮后发送它们。
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();
};
我需要一些帮助将一段 Java 代码移植到 C#。该代码是关于 post 对网络 api 的请求,以建立连接 我在 Java 中使用了 Volley,并且我已经在 Visual Studio 中安装了 NuGet。我在将 StringRequest 函数转换为 C# 时遇到问题。
这是我目前尝试在 C# 中移植的一段代码,但在 C# 中声明参数时出现错误。例如,它无法识别 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# 我会很高兴。
我在不使用 Volley 和移植 java 代码的情况下自行修复了它。
再次抱歉请求移植,我不知道这不是我应该在这里问的问题。感谢 jrswgtr 让我知道。
这是向网络 api 发出简单 POST 请求的代码,从两个编辑框中获取值并在单击按钮后发送它们。
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();
};