【问题标题】:Android equivalent of WebClient in .NETAndroid 等效于 .NET 中的 WebClient
【发布时间】:2011-06-18 19:43:56
【问题描述】:

我前段时间用 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),但只是快速浏览了一下,这似乎并不合适。

有没有人有任何如何移植的示例代码?

【问题讨论】:

    标签: c# .net android webclient


    【解决方案1】:

    这可能是等价的:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2012-11-28
      • 2018-03-30
      • 2010-11-20
      • 2019-04-15
      相关资源
      最近更新 更多