簡體   English   中英

與.NET中的WebClient的Android等效

[英]Android equivalent of WebClient in .NET

我有一個相當基本的應用程序,我前段時間在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開發人員),但是從快速的外觀來看似乎不合適。

有人有任何示例代碼如何將其移植嗎?

這可能是等效的:

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來獲取數據。 有一些教程,你可以閱讀,以幫助您了解了如何使用這些類這樣的一個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM