我有一个相当基本的应用程序,这是我前段时间用C#写的,我想为安卓平台重写它。它只使用了一些web软件提供的应用程序接口,我只需在.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
|安卓开发者),但从快速浏览来看,这似乎并不合适。
有没有人有任何关于如何移植的示例代码?
发布于 2011-06-19 03:49:36
这可能是等效的:
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。
https://stackoverflow.com/questions/6398465
复制相似问题