我有一个相当基础的应用程序,是我之前用 C# NET 编写的,我想为 Android 平台重写它。它只是使用一些 Web 软件公开的 API,我可以通过 .NET 中的 WebClient 访问它。
WebClient myClient = new WebClient();
//Prepare a Name/Value Collection to hold the post values
NameValueCollection form = new NameValueCollection();
form.Add("username", "bob");
form.Add("password", GetMD5Hash("mypass"));
form.Add("action", "getusers");
// POST data and read response
Byte[] responseData = myClient.UploadValues("https://mysite.com/api.php", form);
string strResponse = Encoding.ASCII.GetString(responseData);
我找到了 WebKit (android.webkit
| Android Developers),但快速浏览似乎并不合适。
有没有人有任何关于如何将其移植过来的示例代码?
最佳答案
这可能是等价的:
List<NameValuePair> form = new ArrayList<NameValuePair>();
form.add(new BasicNameValuePair("username", "bob");
// etc...
// Post data to the server
HttpPost httppost = new HttpPost("https://mysite.com/api.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(httppost);
总结HttpClient可以替换您的 WebClient。然后你必须使用 HttpPost用于发布数据或 HttpGet用于检索数据。您可以阅读一些教程来帮助您了解如何像这样使用这些类 one .
关于c# - Android 等同于 .NET 中的 WebClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6398465/