如何用java for android编写此C#方法(apache客户端已弃用)?
我正在为我的c#程序制作一个android版本,考虑到apache客户端已被弃用,我是一个新手,我需要将此方法转换为java 我在c#中使用这个方法连接我站点上的一个php文件,使用POST方法传递用户名和密码,然后使用echo返回结果 我的C#代码是 这是最重要的方法如何用java for android编写此C#方法(apache客户端已弃用)?,java,c#,php,android,apache,Java,C#,Php,Android,Apache,我正在为我的c#程序制作一个android版本,考虑到apache客户端已被弃用,我是一个新手,我需要将此方法转换为java 我在c#中使用这个方法连接我站点上的一个php文件,使用POST方法传递用户名和密码,然后使用echo返回结果 我的C#代码是 这是最重要的方法 private string Submit(string[] parameters) { ServicePointManager.Expect100Continue = false;
private string Submit(string[] parameters)
{
ServicePointManager.Expect100Continue = false;
string arg_0B_0 = string.Empty;
string result;
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(this.serverUrl);
httpWebRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = false;
httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
httpWebRequest.Timeout = 30000;
byte[] array = new byte[0];
string text = string.Empty;
for (int i = 0; i < parameters.Length; i += 2)
{
text += string.Format("{0}={1}&", parameters[i], HttpUtility.UrlEncode(parameters[i + 1]));
}
httpWebRequest.ContentLength = (long)text.Length;
array = Encoding.UTF8.GetBytes(text);
Stream requestStream = httpWebRequest.GetRequestStream();
requestStream.Write(array, 0, array.Length);
requestStream.Close();
WebResponse response = httpWebRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
string text2 = streamReader.ReadToEnd();
response.Close();
result = text2;
}
catch (Exception ex)
{
result = "Error: " + ex.Message;
}
return result;
}
私有字符串提交(字符串[]参数)
{
ServicePointManager.Expect100Continue=false;
string arg_0B_0=string.Empty;
字符串结果;
尝试
{
HttpWebRequest HttpWebRequest=(HttpWebRequest)WebRequest.Create(this.serverUrl);
httpWebRequest.UserAgent=“Mozilla/5.0(兼容;MSIE 9.0;Windows NT 6.1;WOW64;Trident/5.0)”;
httpWebRequest.ContentType=“应用程序/x-www-form-urlencoded”;
httpWebRequest.Method=“POST”;
httpWebRequest.KeepAlive=false;
httpWebRequest.Credentials=CredentialCache.DefaultCredentials;
httpWebRequest.Timeout=30000;
字节[]数组=新字节[0];
string text=string.Empty;
对于(int i=0;i
我搜索过,但大多数答案都使用apache客户端和HttpUrlconction您仍然可以使用不推荐使用的DefaultHttpClient,但建议不要使用不推荐使用的类,因此您可以在Android中使用HttpUrlConnection和AyncTask。REST服务和AsyncTask的HttpUrlConnection,这样所有的网络调用都不会在UI线程上进行。我不知道如何使用HttpUrlConnection,如果有类似于我需要的示例的教程,我将不胜感激。谢谢你,它是用Java编写的,告诉我你是否可以,如果你能将这段代码转换成android版本,如果不能,我会在答案中发布代码
private string Submit(string[] parameters)
{
ServicePointManager.Expect100Continue = false;
string arg_0B_0 = string.Empty;
string result;
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(this.serverUrl);
httpWebRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = false;
httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
httpWebRequest.Timeout = 30000;
byte[] array = new byte[0];
string text = string.Empty;
for (int i = 0; i < parameters.Length; i += 2)
{
text += string.Format("{0}={1}&", parameters[i], HttpUtility.UrlEncode(parameters[i + 1]));
}
httpWebRequest.ContentLength = (long)text.Length;
array = Encoding.UTF8.GetBytes(text);
Stream requestStream = httpWebRequest.GetRequestStream();
requestStream.Write(array, 0, array.Length);
requestStream.Close();
WebResponse response = httpWebRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
string text2 = streamReader.ReadToEnd();
response.Close();
result = text2;
}
catch (Exception ex)
{
result = "Error: " + ex.Message;
}
return result;
}